1

$vs.notify用来显示来自 db 的消息。所以想到在actions.js中使用它。但得到错误为TypeError: Cannot read property 'notify' of undefined. 有什么方法可以在 action.js 文件中使用它。使用的代码在这里:

addTag({
    commit
}, tag) {
    return new Promise((resolve, reject) => {
        Vue.prototype.$http.post('/tags/add/', {
                tag
            })
            .then((response) => {
                commit('ADD_TAG', Object.assign(response.data.data[0]))
                Vue.prototype.$vs.notify({
                    title: 'Success',
                    text: `tag-${response.data.data[0].name} added suceesfully`,
                    iconPack: 'feather',
                    icon: 'icon-alert-circle',
                    color: 'success'
                })
                resolve(response)
            })
            .catch((error) => {
                reject(error)
            })
    })
}

在此处输入图像描述

4

2 回答 2

1

Vuesax没有在 上注册任何内容Vue.prototype,这就是您收到该特定错误消息的原因。似乎该服务被添加到每个组件中。它也不能在模块中导入。因此,作为一种解决方法,您可以自己将其添加到原型中。

将服务添加App.vueVue.prototype

应用程序.vue

<script>
import Vue from 'vue';

export default {
  created() {
    Vue.prototype.$vs = this.$vs;
  }
}
</script>

现在您可以按照您在帖子中尝试的方式使用它:

动作.js

import Vue from 'vue';

Vue.prototype.$vs.notify(...)
于 2021-02-22T20:39:19.627 回答
0

官方文档中,它指出

要添加通知,我们有全局函数 $vs.notify

所以,尝试使用this._vm.$vs.notify或者可能this._vm.notify在你的 vuex 操作中。

于 2021-02-22T18:04:49.210 回答