使用甜美警报吐司进行错误处理VUEX

发布于 2025-02-02 18:54:54 字数 590 浏览 3 评论 0原文

我已经在项目上安装了Sweet Alert。 现在,我想在vuex Action中使用它,但它不起作用。

update({ commit, dispatch }, data) {
  return new Promise(async (resolve, reject) => {
    try {
      const res = await update(data);
      resolve(res.data)
      this.app.$toast('this is a test toas', {
        title: 'Test'
      })
    } catch (err) {
      this.app.$toast('this is a test toas', {
        title: 'Test'
      })
      reject(err.response);
    }
  })
},

它在组件中正常工作,但我无法使用this.app。$ toast访问它。

I have installed sweet alert on my project.
Now i want to use it in Vuex actions but it does not work.

update({ commit, dispatch }, data) {
  return new Promise(async (resolve, reject) => {
    try {
      const res = await update(data);
      resolve(res.data)
      this.app.$toast('this is a test toas', {
        title: 'Test'
      })
    } catch (err) {
      this.app.$toast('this is a test toas', {
        title: 'Test'
      })
      reject(err.response);
    }
  })
},

It works fine in components but i can not access it with this.app.$toast

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

誰ツ都不明白 2025-02-09 18:54:54

$ TOAST仅在component范围vuex中可用,我以前有这样的问题,我将其作为参考参数传递给了吐司。

update({ commit, dispatch }, {data , that}) {
return new Promise(async (resolve, reject) => {
  try {
    const res = await update(data);
    resolve(res.data)
    that.$toast('this is a test toas', {
      title: 'Test'
    })
  } catch (err) {
    that.$toast('this is a test toas', {
      title: 'Test'
    })
    reject(err.response);
  }
})

从组件调用update方法时将此引用传递给

update({
  data,
  that: this
})

The $toast is only available in component Scope not vuex, I had an issue like that before and I passed this as a reference parameter to toast.

update({ commit, dispatch }, {data , that}) {
return new Promise(async (resolve, reject) => {
  try {
    const res = await update(data);
    resolve(res.data)
    that.$toast('this is a test toas', {
      title: 'Test'
    })
  } catch (err) {
    that.$toast('this is a test toas', {
      title: 'Test'
    })
    reject(err.response);
  }
})

while calling the update method from component pass this reference as

update({
  data,
  that: this
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文