【问题标题】:How to call a component function if a data changes in store如果存储中的数据发生更改,如何调用组件函数
【发布时间】:2020-10-26 18:14:33
【问题描述】:

我想在变量状态发生变化时触发一个方法(serviceSelected)。我正在尝试使用计算但它不起作用。我的变量在 vuex 商店中,它的 getter 为 getServiceSelected

computed: {
...mapGetters(["getServiceSelected"]),
serviceChanged() {

  this.serviceSelected(this.getServiceSelected);
  return this.getServiceSelected
}

}

【问题讨论】:

    标签: javascript vue.js vuex computed-properties


    【解决方案1】:

    改用watcher

    computed: {
        ...mapGetters(["getServiceSelected"])
    },
    watch: {
        getServiceSelected(newValue, oldValue) {
            // do something
            this.serviceSelected(newValue);
        }
    }
    

    【讨论】:

    • 新值将被计算为一个,但旧值将是什么?
    • 旧值是数据变化前的值,每次getServiceSelected发生变化,都会调用getServiceSelected watcher,你可以使用newValue(改成什么)和@ 987654326@(它的变化)
    • 好的,它的工作非常感谢你从昨天开始我就被困住了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 2021-03-08
    相关资源
    最近更新 更多