vue.js - 如何在 Vue 模板中的数组中使数据响应

标签 vue.js

<template>
  <button v-on:click="modify"> modify </button>
  <div v-model="lists">{{ lists[0] }}</div>
</template>

<script>
export default {

 methods: {
    modify: function() {
      console.log(this.lists)
      this.lists[0][0] = 2
      console.log(this.lists)
    },

  data: function () {
    return {
      lists: [[1,2,3],[2,3,3]]
    }
  }
}
</script>

模板中的数组似乎没有更新。但是日志控制台已经改变了。

当数据是数组时,如何使其具有反应性?

实际发生的情况:

Before clicking modify
<div v-model="lists">{{ lists[0] }}</div> # produce 1
After clicking modify
<div v-model="lists">{{ lists[0] }}</div> #produce 1

预期结果:

Before clicking  modify
<div v-model="lists">{{ lists[0] }}</div> # produce 1
After clicking modify
<div v-model="lists">{{ lists[0] }}</div> #produce 2

最佳答案

这是一个caveat在 Vue.js 中通过索引更新数组时试试这个。

this.lists[0].splice(0,1,2)

关于vue.js - 如何在 Vue 模板中的数组中使数据响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43243637/

相关文章:

javascript - 在vuejs中使用 `id`时,如何获取 `cname`值作为选项值和 `v-select`作为选项标签?

javascript - vue导入highcharts-more时遇到问题

javascript - Vue.js 和 Vuex - 将访问 token 存储在商店中并将用户重定向到 URL 进行登录

vue.js - 组件上触发代码 v-show=true

vue.js - 如何结合 v-for 和 v-model 来编辑对象列表?

javascript - 在 Vue 中获取模板作为字符串

vue.js - 在Vue中: How do you implement the cell-class-name and row-class-name in the el-table of ElementUI?

javascript - 通过 Vue.Js 调查导航

vue.js - vuejs webpack npm run watch

javascript - 从对象访问数组