Skip to content

watch() doesn't trigger when adding new property with Vue.set( ) #12967

Closed
@depressedX

Description

@depressedX

Version

2.7.14

Reproduction link

codepen.io/AxisZ/pen/RwYrmxq

Steps to reproduce

Just open the codepen link, and open browser's devtool console

What is expected?

The Vue.set(obj, 'added', 1); statement should trigger the watcher callback, which will print [watch getter]obj changed.

What is actually happening?

nothing happened, the watcher does not be triggered


This behavior is not the same as neither vue2.6 or vue3.
Open this codepen writting with vue2.6+@vue/composition-api, It will print [watch getter]obj changed, which is expected.

vue2.6+@vue/composition-api demo: https://codepen.io/AxisZ/pen/VwGeOEE

The core codes is here:

const { watch, reactive, set } = Vue;

const obj = reactive({ exist: 1 });

watch(
    () => obj,
    () => {
        console.log('[watch getter]obj changed');
    },
    { deep: true },
);

setTimeout(() => {
    console.log('should print watcher message↓↓↓↓');
  // add a new property, should trigger that watch below as expected
    set(obj, 'added', 1);
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions