arrays - Vuejs 从数组创建列表
问题描述
我在使用 Vuejs 创建列表时遇到了一个奇怪的问题。我有一个像这样的数组:
const sr = new Vue({
el: '#singlerecipe-app',
data: {
checkeditems: [],
},
});
这是在我的 HTML 中实例化的,如下所示:
<div class="container" id="singlerecipe-app">
<singlerecipe :checkeditems="checkeditems"></singlerecipe>
</div>
并在我的组件中声明为 Prop:
Vue.component('singlerecipe', {
props: ['checkeditems'],
template: `
<ul>
<li v-for="item of checkeditems">
{{ item.checkeditems }}
</li>
</ul>
`
})
检查项目代码为:
<li v-if="result.strIngredient1">
<input type="checkbox" :value="result.strIngredient1" :id="result.strIngredient1" v-model="checkeditems"> {{result.strIngredient1}} - {{result.strMeasure1}}
</li>
<li v-if="result.strIngredient2">
<input type="checkbox" :value="result.strIngredient2" :id="result.strIngredient2" v-model="checkeditems"> {{result.strIngredient2}} - {{result.strMeasure2}}
</li>
如果我单击其中一个复选框,它肯定会向数组 checkeditems[] 添加正确的值,但奇怪的是,它会创建新的列表项但不会添加值,所以我最终得到了这个:
<ul>
<li></li>
<li></li>
</ul>
列表项中没有值。有谁知道我做错了什么?
解决方案
我想你只是不小心添加了一个“checkeditems”。
它应该是:
Vue.component('singlerecipe', {
props: ['checkeditems'],
template: `
<ul>
<li v-for="item of checkeditems">
{{ item }}
</li>
</ul>
`
})
推荐阅读
- javascript - 如何在nodejs中构造多个单页表单并处理它们
- java - 在不更改订单的情况下获取唯一列表的最佳方法
- c++ - 使用预处理器生成默认值
- javascript - 如何在 Leaflet JS 中获取外部 URL JSON
- javascript - 过滤表数据
- javascript - 从文本文件中获取数据到 HTML 页面
- python - 如何在 Geopandas 中将非英文字符显示为标签?
- python - 如何将多个 XML 文件解析为多个 CSV 文件?
- javascript - 我如何使用 Intersection Observers 来判断某物何时占用了整个视口?
- java - 如何在 android studio 中安装 64 位版本的 Realm?