【问题标题】:Calling a mixin function from the store从 store 调用 mixin 函数
【发布时间】:2018-10-27 11:59:46
【问题描述】:

我正在尝试从 vue 组件调用 api。我正在通过 vuex 商店拨打电话(因为我读到这是一个很好的做法)。

在 store 操作中,我想与我在 vue 实例中作为 mixin 函数的错误处理函数进行交互。但只有当我将完整的 vue 实例传递给 store 调用时,我才能这样做:this.$store.dispatch('store/get', this);

对 mixin 的调用是这样的:

get: function(context, object) {
       ...
            object.catchError(error)

我想避免在调度中传递this,但仍然能够访问mixin函数。这是可能的还是我应该以不同的方式做事?

【问题讨论】:

    标签: vue.js mixins vuex


    【解决方案1】:

    试试这个: 像这样调用动作:

    this.$store.dispatch('store/get')
     .then(r => {
            // r contains the result, but you can call here the getter as well..
        })
     .catch(e => {
            // catch every error here, you can handle here the error.
    });
    

    而getter函数是这样的:

    get: function(context) {
       return axios.get(/api/call/get)
        .then(r => {
            ... // mutations, conditions of data check.. throwing errors..
            return r;
        })
        .catch(e => {
            throw e;
        })
    }
    

    我希望我的想法很清楚,但如果不是,请评论您的问题! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-24
      • 1970-01-01
      • 2021-01-08
      • 1970-01-01
      • 2019-08-08
      • 2018-04-10
      • 2014-05-10
      • 2018-02-12
      相关资源
      最近更新 更多