vue-interview

## Vue组件为什么只能有一个根元素?

这题现在有些落伍,vue3已经不用一个根了。因此这题目很有说头!


体验一下

vue2直接报错,test-v2.html

new Vue({
  components: {
    comp: {
      template: `
        <div>root1</div>
        <div>root2</div>
      `
    }
  }
}).$mount('#app')


vue3中没有问题,test-v3.html

Vue.createApp({
  components: {
    comp: {
      template: `
        <div>root1</div>
        <div>root2</div>
      `
    }
  }
}).mount('#app')


回答思路


范例


知其所以然