How to make data reactive when it's array in V

2019-07-31 11:32发布

<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>

The array at template does not seems to get updated. But the log console has changed.

How do you make a data reactive when it's an array?

What is actually happening:

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

What is expected:

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

标签: vue.js
1条回答
等我变得足够好
2楼-- · 2019-07-31 12:24

This is a caveat when updating arrays by index in Vue. Try this.

this.lists[0].splice(0,1,2)
查看更多
登录 后发表回答