vue.js中的变量不是反应性的
为什么我的网格
变量没有反应性?在toggle(index)
方法中 逻辑,console.log(this.grid)
正在打印正确的输出,但grid
变量未更新
<template>
<div class="game-container">
<div>{{ grid }}</div>
<div class="board">
<div
@click="toggle(index)"
ref="squares"
v-for="(item, index) in grid"
:key="index"
class="square"
:class="{ 'on': item }">
{{ item }} {{ index }}
</div>
</div>
</div>
</template>
<script>
export default {
name: 'GameComponent',
data() {
return {
size: 5,
grid: Array(25).fill(true),
}
},
methods: {
toggle(index) {
const C = index
const N = index - this.size >= 0 ? index - this.size : null
const E = (index + 1) % this.size != 0 ? index + 1 : null
const S = index + this.size < this.size * this.size ? index + this.size : null
const W = index % this.size != 0 ? index - 1 : null
this.grid[C] = !this.grid[C]
if (N) this.grid[N] = !this.grid[N]
if (E) this.grid[E] = !this.grid[E]
if (S) this.grid[S] = !this.grid[S]
if (W) this.grid[W] = !this.grid[W]
console.log(this.grid)
}
}
}
</script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
height: 100%;
}
body {
height: 100%;
}
.game-container {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.board {
display: flex;
flex-wrap: wrap;
width: 40rem;
}
.square {
width: 8rem;
height: 8rem;
border: 2px solid red;
cursor: pointer;
}
.on {
background-color: black;
color: white;
}
.hover {
background-color: orange;
}
</style>
Why is my grid
variable not reactive? In the toggle(index)
method, after
the logic, the console.log(this.grid)
is printing the correct output but the grid
variable isn't updating
<template>
<div class="game-container">
<div>{{ grid }}</div>
<div class="board">
<div
@click="toggle(index)"
ref="squares"
v-for="(item, index) in grid"
:key="index"
class="square"
:class="{ 'on': item }">
{{ item }} {{ index }}
</div>
</div>
</div>
</template>
<script>
export default {
name: 'GameComponent',
data() {
return {
size: 5,
grid: Array(25).fill(true),
}
},
methods: {
toggle(index) {
const C = index
const N = index - this.size >= 0 ? index - this.size : null
const E = (index + 1) % this.size != 0 ? index + 1 : null
const S = index + this.size < this.size * this.size ? index + this.size : null
const W = index % this.size != 0 ? index - 1 : null
this.grid[C] = !this.grid[C]
if (N) this.grid[N] = !this.grid[N]
if (E) this.grid[E] = !this.grid[E]
if (S) this.grid[S] = !this.grid[S]
if (W) this.grid[W] = !this.grid[W]
console.log(this.grid)
}
}
}
</script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
height: 100%;
}
body {
height: 100%;
}
.game-container {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.board {
display: flex;
flex-wrap: wrap;
width: 40rem;
}
.square {
width: 8rem;
height: 8rem;
border: 2px solid red;
cursor: pointer;
}
.on {
background-color: black;
color: white;
}
.hover {
background-color: orange;
}
</style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
VUE中的阵列不是通过设计反应性的。它有充分记录的 https:///v2.vuejs.s.s.s.org/v2/指南/反应性。
但是,这些天谁读了手册,对吧?
Arrays in Vue are not reactive by design. It is well documented https://v2.vuejs.org/v2/guide/reactivity.html#For-Arrays, with solutions.
But who reads manuals these days, right?)