简体   繁体   中英

Common functions are not executed in Nuxt.js store

Assumption

I wanted to use inject in Nuxt.js to share the same process, and I wanted to use it in Vuex, so I tried to use it in store, but it would not run. How can I use it?

What we want to achieve

I want to execute common functions in store.

Code

store

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

plugin/responsePocessing

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'
  ],

・
・
・

Error

No error.

the best solution that comes to my mind is to define your errorHandling function in the utils directory, then you can use import to import it in your plugin and import it in your store file seperately.

but if you are not going to use it inside your components or pages i recommend not to create a plugin at all. just create a file in your utils directory, like processUtils.js, then import it in your store.

the thing is plugins where created in nuxt so you can inject functions needed inside your template and script tags, store does not have access to plugins

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM