Vue two-dimensional data content change solution to refresh the view

The content of the two-dimensional array in vue changes, the watch method and refresh view will not be triggered

Adding deep to watch can't solve it

 

method one

Remount, although the view will be refreshed but the watch method will not be triggered

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <script src="./vue.js" charset="utf-8"></script>
</head>

<body>
    <div id='app'>
        {{arr}}
        <button @click="change">改变</button>
    </div>

    <script type="text/javascript">
        new Vue({
            el: "#app",
            data: {
                arr: [
                    ['0', '0'],
                    ['0', '0']
                ]
            },
            watch: {
                arr: {
                   // deep: true,
                    handler: function(newVal, oldVal) {
                        console.log(arguments)
                    }
                }
            },
            methods: {
                change: function() {
                    this.arr[0][0] = '1'
                    // 重新挂载实例,实现数据刷新
                    this.$mount("#app")
                }
            }
        })
    </script>
</body>

</html>

 

Method Two:

Use vue's set method

this.$set(this.arr[0], '0', 1)

This method will trigger the watch event and also update the view

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325083808&siteId=291194637