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

后端 未结 8 2049
予麋鹿
予麋鹿 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 <tr/> 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.

    0 讨论(0)
  • 2020-11-29 05:05

    Just make sure that you have one root div and put everything inside this root

      <div class="root">
        <!--and put all child here --!>
       <div class='child1'></div>
       <div class='child2'></div>
      </div>
    

    and so on

    0 讨论(0)
提交回复
热议问题