Home > front end >  Common functions are not executed in Nuxt.js store
Common functions are not executed in Nuxt.js store

Time:05-08

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.

CodePudding user response:

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

  • Related