javascript - Vue 数组内容没有更新
问题描述
我想使用我创建的函数向此列表添加一个数字,但是数据不会更新。for 循环将数据添加到列表中,但上面的代码没有使用它。
<script>
export default {
data: () => ({
row: [],
column: [],
}),
methods: {
async initGraph() {
for(let x = 0; x < 25; x++)
{
this.row[x] = x;
}
for(let y = 0; y < 25; y++){
this.column[y] = y;
}
console.log(this.row);
console.log(this.column);
}
},
mounted(){
this.initGraph();
}
}
</script>
<template>
<v-app>
<tbody>
<tr v-for="r in row" v-bind:key="r" :id="r">
<td v-for="c in column" v-bind:key="c" :id="c" class="unvisited">
</td>
</tr>
</tbody>
<h1>{{row}}</h1>
</v-app>
</template>
解决方案
如果直接修改数组,Vue 无法检测到数组的更改。请参阅https://vuejs.org/v2/guide/list.html#Mutation-Methods 一种可能的解决方案是使用数组操作的方法。以下应该有效:
<script>
export default {
data: () => ({
row: [],
column: [],
}),
methods: {
initGraph() {
for(let x = 0; x < 25; x++)
{
this.row.push(x);
}
for(let y = 0; y < 25; y++){
this.column.push(y);
}
console.log(this.row);
console.log(this.column);
}
},
mounted(){
this.initGraph();
}
}
</script>
<template>
<v-app>
<table>
<tbody>
<tr v-for="r in row" v-bind:key="r" :id="r">
<td v-for="c in column" v-bind:key="c">{{c}}</td>
</tr>
</tbody>
</table>
</v-app>
</template>
推荐阅读
- java - 尝试生成 Spring Cloud 合约时找不到 ContractVerifierUtil 类
- typescript - 'string' 类型的参数不能分配给 Lodash/fp orderBy 的第二个参数中的 'LoDashStatic' 类型的参数
- reactjs - 如何在 React 中创建一个通用的 Context.Provider?
- angular - ag-grid 自定义加载器作为指令
- python-3.x - 将 Git 移动到另一个文件夹,但我遇到了问题
- google-apps-script - 将特定数据复制到其他工作表(带脚本)
- reactjs - 在 ReactJs 中显示产品数组
- batch-file - 如何删除批处理文件中的新行?
- android - 单击箭头时如何为展开/折叠箭头设置 180 度动画?
- c# - 如何在 Linq C# 中使用 CTE?