Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

关于 Vue 中的 watch $set 的用法

问题 :
代码如下,执行代码后, handler 函数会执行,输出 undefined。但是this.$set(this.data, 'checked', true); 这行代码操作的是 data对象并没有操作 children 属性。为什么会触发 handler 函数?

<script>
    export default {
        name: "watch",
        data () {
            return {
                data: { // 初始值没有 children 属性
                    title: '哈哈'
                }
            }
        },
        watch: {
            'data.children': { // 在这里监听 children 属性
                handler: function (value) {
                    console.log(value);
                },
                deep: true //深度监听
            }
        },
        mounted () {
           // 给 data 添加新的属性 checked 并赋值为 true
            this.$set(this.data, 'checked', true);
        }
    }
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

我把你贴的代码运行了一边,没有发现handler函数有执行


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...