javascript - Vue 组件在“手动”多次使用时不会呈现......但在循环重复时有效
问题描述
我有如下代码。前 5 个组件正确渲染,但接下来的 3 个未正确渲染。仅呈现这 3 个中的第一个:
const test = {
data() {
return {
count: 0
};
},
props: {
text: {
type: Number,
required: true
}
},
template: `
<button @click="count++">
[ {{text}} ] You clicked me {{ count }} times.
</button>
`
};
const app = Vue.createApp({});
app.component("test", test);
app.mount("#app");
<!DOCTYPE html>
<html>
<body>
<div id="app">
<div v-for="i in 5">
<test :text="i" />
</div>
<hr />
<test :text="99" />
<test :text="42" />
<test :text="55" />
</div>
<script
id="vue"
src="https://unpkg.com/vue@3.0.11/dist/vue.global.prod.js"
></script>
</body>
</html>
显然......我希望能够不仅在循环中使用组件。我错过了什么?
顺便说一句,我能够用最新的 vue 2.x 重现同样的行为(vue 应用程序初始化代码略有不同,ofc)
解决方案
该问题是由自闭合元件引起的。请参阅下面的更新演示...
为什么
在 HTML5 规范中,仅允许在“void”元素上使用自闭合标签(Void 元素是那些可能不包含任何内容的元素 - asbr
等img
) - 请参阅这个 SO question - Are (non-void) self-closing tags valid在 HTML5 中?
因此,这里发生的情况是浏览器会解释您的无效 HTML(当页面加载时 - 甚至在 Vue 启动之前),就像这样(只需运行您的代码片段,注释掉app.mount()
并在开发工具中检查 HTML):
<div id="app">
<div v-for="i in 5">
<test :text="i"></test>
</div>
<hr>
<test :text="99">
<test :text="42">
<test :text="55">
</test>
</test>
</test>
</div>
现在很容易看出为什么v-for
工作正常,第一个组件渲染正常,但其余的不是(test
组件没有默认插槽)
请注意,仅在使用 DOM 内模板并且与字符串模板(选项)或 SFC 文件(并且 Vue ESLint 规则和Vue 样式指南template
实际上推荐使用自闭合标签)时,这才是问题
const test = {
data() {
return {
count: 0
};
},
props: {
text: {
type: Number,
required: true
}
},
template: `
<button @click="count++">
[ {{text}} ] You clicked me {{ count }} times.
</button>
`
};
const app = Vue.createApp({});
app.component("test", test);
app.mount("#app");
<!DOCTYPE html>
<html>
<body>
<div id="app">
<div v-for="i in 5">
<test :text="i"></test>
</div>
<hr />
<test :text="99" ></test>
<test :text="42" ></test>
<test :text="55" ></test>
</div>
<script
id="vue"
src="https://unpkg.com/vue@3.0.11/dist/vue.global.prod.js"
></script>
</body>
</html>
推荐阅读
- android - Kotlin Channels 在片段之间发送事件
- c - C: unsigned long int 的行为不符合预期
- react-native - React Native Maps - 标注不显示内部图像
- biztalk - BizTalk - 推广多个值,订阅其中一个
- python - Django 教程应用程序,第 2 部分。为什么缺少 - 在 makemigrations 命令后添加字段 Question to Choice?
- flutter - 颤振:RaisedButton 比它的 SizedBox 大
- javascript - 为什么时刻转换此日期不正确?
- batch-file - 如何在批处理中正确使用重命名(ren)
- sql - DB2 - 自治存储过程
- jquery - 使侧边栏在页面滚动时保持粘性