vue.js - 一个组件的多个实例被解析为一个
问题描述
考虑一下:
<div id="app">
<test-component to="abc">Some ABC</test-component>
<test-component to="xyz">Some XYZ</test-component>
<test-component to="123">Some 123</test-component>
</div>
----
Vue.component('test-component', {
template: '<a :href="to"> <slot></slot> </a>',
props: ['to'],
});
new Vue({
el: "#app",
})
所有三个“测试组件”都按预期呈现。见https://jsfiddle.net/gotp1fdL/1/
但是,当我实现一个包装 App 组件时,只有第一个“测试组件”被渲染,其他的甚至被忽略......见https://jsfiddle.net/gsxq9ajc/
<div id="app">
<app></app>
</div>
<template id="application">
<test-component to="abc">Some ABC</test-component>
<test-component to="xyz">Some XYZ</test-component>
<test-component to="123">Some 123</test-component>
</template>
---
Vue.component('test-component', {
template: '<a :href="to"> <slot></slot> </a>',
props: ['to'],
});
Vue.component('app', {
template: '#application',
});
new Vue({
el: "#app",
})
请问有人可以解释为什么吗?让所有组件独立呈现而没有问题,缺少什么?
解决方案
问题如下,如控制台所示:
vue.js:634 [Vue 警告]:编译模板时出错:
组件模板应该只包含一个根元素。如果您在多个元素上使用 v-if,请改用 v-else-if 链接它们。
在您的应用程序组件中添加包装器将修复它。
<template id="application">
<div>
<test-component to="abc">Some ABC</test-component>
<test-component to="xyz">Some XYZ</test-component>
<test-component to="123">Some 123</test-component>
</div>
</template>
推荐阅读
- google-cloud-dataprep - 如何重命名 dataprep 中的多个列?
- android - java.lang.UnsatisfiedLinkError: dlopen libffmpeg.so: 有文本重定位
- python - 在python的SQL命令中用变量替换整数
- visual-studio - 选择加入预览版 SDK
- android - Android gradle依赖冲突和资源重复值
- kotlin - 如何用http4k在中间建模一个路径参数
- angularjs - Angular 1.4 应用程序中的 React 组件
- c# - iOS webapp 闪屏缓存
- python - SQLalchemy:自动对列应用过滤器
- c - 将指针数组传递给C中的函数