[英]How to use new Vue in a single file component?
I'm still learning Vue.js and I have a minor issue: I have a single file component with an array of checkboxes, and I have looked at the documentation for using multiple checkboxes, but the example there requires me to declare:我仍在学习 Vue.js 并且我有一个小问题:我有一个带有复选框数组的单个文件组件,并且我查看了使用多个复选框的文档,但是那里的示例需要我声明:
new Vue({
el: '#example-3',
data: {
checkedNames: []
}
})
Edit However I have set this up in my single file component within the <script>
tag within data(), but it just checks/unchecks all boxes at once (but feeds back true/false correctly):编辑但是我已经在 data() 的
<script>
标记内的单个文件组件中设置了这个,但它只是一次检查/取消选中所有框(但正确反馈真/假):
<script> export default { name: 'PhrasesDetail', data () { return { game: '', language: '', checkedPhrasesArr: { el: '#selection', data: { checkedPhrasesArr: [] } } } }, ... </script>
The Question is where and how do I declare the checkbox array so that it reacts/recognises the individual elements?问题是我在哪里以及如何声明复选框数组,以便它反应/识别单个元素?
These are my checkboxes:这些是我的复选框:
<tr v-for="(keyTransPair, index) in this.language.data">
<td id="selection"><input type="checkbox" :id="index" v-model="checkedPhrasesArr"></td>
<td>{{index}}</td>
...
</tr>
I have assembled a complete example.我已经组装了一个完整的例子。 (I don´t know anything about your
language.data
object so I´m just using fake data). (我对您的
language.data
对象一无所知,所以我只是使用假数据)。
<template>
<div>
<!-- Your Checkboxes -->
<table>
<tr v-for="(keyTransPair, index) in language.data">
<td id="selection"><input type="checkbox" :value="keyTransPair" :id="index" v-model="checkedPhrasesArr"></td>
<td>{{index}}</td>
</tr>
</table>
<!-- Show the selected boxes -->
{{ checkedPhrasesArr }}
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
// Your language data
language: {
data: {
keyOne: "one",
keyTwo: "two",
keyThree: "three"
}
},
// The Checkbox data
checkedPhrasesArr: []
}
}
}
</script>
Note that the checkbox values are bound with :value="keyTransPair"
.请注意,复选框值与
:value="keyTransPair"
绑定。 You can change the value to anything you want.您可以将值更改为您想要的任何值。 This value will be added to the array if the checkbox is checked.
如果选中该复选框,该值将被添加到数组中。
By the way: You use <td id="selection">
within the v-for
loop.顺便说一句:您在
v-for
循环中使用<td id="selection">
。 So the id "selection" will not be unique.所以 id "selection" 不会是唯一的。 You should better use a class instead of an id here.
您最好在这里使用类而不是 id。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.