切换到嵌套路由会自动运行父级商店中的最后一个提交事件

时间:2019-08-07 04:22:42

标签: vuejs2 vuex vue-router vuex-modules

复制链接 https://codesandbox.io/s/vue-template-c2gtb

复制步骤

Open https://codesandbox.io/s/vue-template-c2gtb
Click on 'Go to Page with nested router' link
Click on 'commit event', it will show an alert box
Now change the nested route by clicking 'change nested route to reporting'

期望什么? 路由更改为报告,但未显示“您好”警报框。

实际情况是什么? 它再次显示“ hello”警报框。

1 个答案:

答案 0 :(得分:1)

您应该更改观察者以忽略旧值与新值相同的情况:

    watch: {
        event: {
            handler(newVal,oldVal) {
                if(oldVal === newVal) return; // <--- the important part
                switch (this.event.id) {
                    case 'new_requirement': {
                        alert('hello');
                        break;
                    }
                    default:
                }
            },
        },
    },