Vue js error: Component template should contain exactly one root element

后端 未结 8 2103
予麋鹿
予麋鹿 2020-11-29 04:37

I don\'t know what the error is, so far I am testing through console log to check for changes after selecting a file (for uploading).

When I run $ npm run watc

8条回答
  •  北海茫月
    2020-11-29 05:04

    if, for any reasons, you don't want to add a wrapper (in my first case it was for components), you can use a functionnal component.

    Instead of having a single components/MyCompo.vue you will have few files in a components/MyCompo folder :

    • components/MyCompo/index.js
    • components/MyCompo/File.vue
    • components/MyCompo/Avatar.vue

    With this structure, the way you call your component won't change.

    components/MyCompo/index.js file content :

    import File from './File';
    import Avatar from './Avatar';   
    
    const commonSort=(a,b)=>b-a;
    
    export default {
      functional: true,
      name: 'MyCompo',
      props: [ 'someProp', 'plopProp' ],
      render(createElement, context) {
        return [
            createElement( File, { props: Object.assign({light: true, sort: commonSort},context.props) } ),
            createElement( Avatar, { props: Object.assign({light: false, sort: commonSort},context.props) } )
        ]; 
      }
    };
    

    And if you have some function or data used in both templates, passed them as properties and that's it !

    I let you imagine building list of components and so much features with this pattern.

提交回复
热议问题