vue.js - 如何使用 VueJS 列出多个组件?
问题描述
我已经用 vue.js 创建了待办事项列表,我想每隔 4 个列表项添加一些图像,所以我认为使用 v-html 和数组,其中包含与文本绑定的元素以及带有元素的图像,对吗?
例如)
var arr = [<div v-bind:foo><div>,
<div v-bind:foo><div>,
<div v-bind:foo><div>,
<img src="..."/>]
这是待办事项列表(使用 v-html 之前)
解决方案
在您的列表循环中执行以下操作
<li v-for="todo in todos | orderBy 'done'">
<del v-if="todo.done">
{{ todo.text }}
</del>
<template v-else>
{{ todo.text }}
</template>
<img v-bind:src="todo.image" alt="test" v-if="todo.image"/>
<a href="" @click.prevent="todo.done = !todo.done">✓</a>
</li>
并在列表的长度中添加一个观察者,如果它是第四个,则将图像添加到最后一个待办事项:
(function () {
new Vue({
el: '#todo-list',
data: {
todos: [
{
text: 'Do stuff',
done: false,
image: ''
},
{
text: 'Done thing',
done: true,
image: ''
},
{
text: 'Other stuff',
done: false,
image: ''
}
]
},
watch: {
todos: function (list) {
if(list.length%4 == 0) {
this.todos[list.length-1].image = 'https://www.w3schools.com/w3css/img_lights.jpg';
}
}
},
methods: {
new: function () {
this.todos.push({
text: this.todoText,
done: false,
image: ''
});
this.todoText = '';
}
}
});
}());
推荐阅读
- android-studio - 点击“运行”后没有部署菜单,无法编译模拟
- laravel - 为什么我在运行“php artisan”命令时会出现这个错误?
- xamarin.forms - 如何在 xamarin.forms 中使用 FilePicker 过滤 .zip 文件
- c# - 集成 3D 交互式演示小部件问题
- hive - 从 spark 读取 hive 外部表时出现额外的标题?
- java - 如何配置我的 Web 应用程序以运行/读取我的自定义 TrustStore?
- amazon-cloudwatch - AWS CloudWatch 日志计数问题
- matlab - MATLAB:使用 For 循环绘制下标
- c# - API URL 在浏览器中有效,但在 REST 中无效。它给出“响应状态代码不表示成功:404(未找到)。”
- python - 将 retrain.py 的输出转换为 tensorflow.js