我的Maincomponent.vue中有一个data()对象。在那个data()中,我有一个名为myDatabase的数组,它有几个对象。我想使用方法访问myDatabase中的字段 我想在点击时将isPreview切换为false和true
&
答案 0 :(得分:0)
这是documentation about methods:
示例:
<template>
<p>{{ myDatabase[0].isPreview }}</p>
<button @click="reversePreview(1)">Reverse preview</button>
</template>
<script>
export default {
data () {
return {
myDatabase: [{
id: 1,
name: "Blue",
fabric: require("../static/images/blue.jpeg"),
previewImage: require("../static/images/blue-shirt.jpeg"),
isPreview: true
},
{
id: 2,
name: "Black",
fabric: require("../static/images/black.jpeg"),
previewImage: require("../static/images/black-shirt.jpeg"),
isPreview: false
}]
}
},
methods: {
reversePreview (id) {
const index = this.myDatabase.findIndex(db => db.id === id)
this.myDatabase[index].isPreview = !this.myDatabase[index].isPreview
}
}
}
</script>