【发布时间】:2020-06-29 22:07:27
【问题描述】:
所以我从昨天开始就为此头疼。我想要某种两种方式的数据绑定。我希望我的数组 data(): exploredFields 能够更新子组件的值。但是更新是从孩子那里调用的。
这是我的父组件。
<div>
<button @click="resetMinefield(rows, mineCount)">RESET</button>
</div>
<div class="minefield">
<ul class="column" v-for="col in columns" :key="col">
<li class="row" v-for="row in rows" :key="row">
<Field
:field="mineFields[(row + (col - 1) * rows) - 1].toString()"
:id="(row + (col - 1) * rows) - 1"
:e="exploredFields[(row + (col - 1) * rows) - 1]" // This doesn't update the child !!!
@update="exploreField"
/>
</li>
</ul>
</div>
...
<script>
import Field from "@/components/Field";
export default {
name: "Minesweeper",
components: {
Field,
},
created() {
this.resetMinefield(this.rows, this.mineCount);
},
data() {
return {
rows: 7,
columns: 7,
mineCount: 5,
mineFields: [],
exploredFields: [],
}
},
methods: {
exploreField(index) {
this.exploredFields[index] = true;
},
resetMinefield(fieldSize, mineCount) {
// Updates this.mineFields
// Passes data properly to the child
}
},
}
</script>
这是一个子组件。在@click 上,它会更新自我data: explored 和父母data: exploredFields。但是props: e 的动态绑定不起作用。
<template>
<div :class="classObject" @click="changeState">
{{ field }}
</div>
</template>
<script>
export default {
name: "Field",
data() {
return {
explored: false,
}
},
props: {
id: {
type: Number,
default: -1,
},
field: {
type: String,
default: 'X',
},
e: {
type: Boolean,
default: false,
}
},
methods: {
changeState() {
this.$emit("update", this.id);
this.explored = true;
}
},
computed: {
classObject: function() {
// Stuff here
}
},
}
</script>
我还尝试对数据 :explored 而不是 props :e 进行动态绑定,但也没有效果。它似乎不想更新,因为我正在从孩子那里调用更新。即使我可以看到数据发生变化,它也不会动态地传回给孩子
【问题讨论】:
-
您似乎没有在
Field组件中的任何地方使用e。
标签: vue.js