簡體   English   中英

Nuxt.js store 不執行常用功能

[英]Common functions are not executed in Nuxt.js store

假設

想在Nuxt.js中使用inject共享同一個進程,想在Vuex中使用,於是嘗試在store中使用,但是運行不了。 我該如何使用它?

我們想要達到的目標

我想在商店中執行常用功能。

代碼

店鋪

this.$axios.$post(`${url.POST_API}poss`, {
      post: {
・
・
・
・
      .catch(() => {
        // commit('alertSwitchError', true)
        // setTimeout(() => {
        //   commit('alertSwitchError', false)
        // }, 3000)
        this.$errorHandling
      })
  }

插件/響應處理

const errorHandling = ({ store }) => {
  store.commit('alertSwitchError', true)
  setTimeout(() => {
    store.commit('alertSwitchError', false)
  }, 3000)
}

export default ({ app }, inject) => {
  inject('errorHandling', errorHandling)
}

nuxt.config.js

・
・
・
  plugins: [
・
・
    'plugins/responsePocessing'
  ],

・
・
・

錯誤

沒有錯誤。

我想到的最佳解決方案是在 utils 目錄中定義您的 errorHandling function ,然后您可以使用 import 將其導入到您的插件中,並將其單獨導入到您的商店文件中。

但如果您不打算在您的組件或頁面中使用它,我建議您根本不要創建插件。 只需在您的 utils 目錄中創建一個文件,例如 processUtils.js,然后將其導入您的商店。

事情是在 nuxt 中創建的插件,因此您可以在模板和腳本標簽中注入所需的功能,商店無法訪問插件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM