美文网首页
vue改变数组或对象视图没更新

vue改变数组或对象视图没更新

作者: 我背井离乡了好多年 | 来源:发表于2021-06-21 22:19 被阅读0次

调用方法:Vue.set( target, key, value )进行解决

<template>
  <div style="width: 100%;height: 100%;background-color: #235649;">
    <a-button @click="cli">点我</a-button>
    {{arr}}
  </div>
</template>
<script>
  // import Vue from 'vue'

  export default {
    data() {
      return {
        arr: {
          name: "111",
          age: 18
        }
      }

    },
    methods: {
      cli() {
        // 对象是新增属性无法监听
        // 数组是改变其中一个元素 如arr[0]
        // this.arr.xin = '888'
        this.$set(this.arr,'xin','888')
        console.log(this.arr)
      }
    }
  }
</script>
<style lang="less">


</style>

相关文章

网友评论

      本文标题:vue改变数组或对象视图没更新

      本文链接:https://www.haomeiwen.com/subject/qsxvyltx.html