javascript - Accessing Nuxt plugin function in vuex getter - Stack Overflow

I am getting started with nuxtjs and vuex. I just encountered an issue accessing a bined injected plugi

I am getting started with nuxtjs and vuex. I just encountered an issue accessing a bined injected plugin function from a getter. E.g.:

nuxt.config.js

...
plugins: [
  '~/plugins/myplugin.js'  
  ],
...

~/plugins/myplugin.js

function doSomething(string) {
        console.log("done something: " + string)
    }

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

~/store/index.js

export const actions = {
    someAction({mit}) {
        this.$doSomething("Called from Action") // works
    }
}

export const getters = {
    someGetter: state => {
        this.$doSomething("Called from Getter") // throws error
    }
}

The code works for the action someAction but the call in getter someGetter will result in a error suggesting this is undefined.

The nuxt documentation only shows examples for accessing injected plugin functions from mutations and actions but does not explicitly mention that getters can not access plugin functions. Is this even possible in nuxt or is there a good reason not to call a plugin method in a getter? Any help appreciated.

I am getting started with nuxtjs and vuex. I just encountered an issue accessing a bined injected plugin function from a getter. E.g.:

nuxt.config.js

...
plugins: [
  '~/plugins/myplugin.js'  
  ],
...

~/plugins/myplugin.js

function doSomething(string) {
        console.log("done something: " + string)
    }

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

~/store/index.js

export const actions = {
    someAction({mit}) {
        this.$doSomething("Called from Action") // works
    }
}

export const getters = {
    someGetter: state => {
        this.$doSomething("Called from Getter") // throws error
    }
}

The code works for the action someAction but the call in getter someGetter will result in a error suggesting this is undefined.

The nuxt documentation only shows examples for accessing injected plugin functions from mutations and actions but does not explicitly mention that getters can not access plugin functions. Is this even possible in nuxt or is there a good reason not to call a plugin method in a getter? Any help appreciated.

Share Improve this question asked May 4, 2020 at 13:43 ValentinValentin 531 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You shouldn't try to do_something in a getter. Getters are for deriving state from your store's state only. The point being that you don't have to separately store and keep up-to-date the derived state. For example you have a list of tasks and you have a getter for pleted tasks instead of a separate list of done tasks: pletedTasks: state => state.tasks.filter(task => task.pleted).

Mutations should only be used for mutating your store's state.

Finally, in actions you can interact with whatever you need, like a webserver API somewhere or this.$do_something and then fire off mutations to update the state based on the results.

So I'd say it makes sense what Nuxt is doing here, to inject in the actions but not to the getters.

I faced the same issue and I was able to get around it by doing

export const getters = {
  someGetter: state => {
    $nuxt.$doSomething("Called from Getter") // Grab $nuxt from the global scope
  }
}

Not sure if this works in SSR mode however

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745362965a4624445.html

相关推荐

  • javascript - Accessing Nuxt plugin function in vuex getter - Stack Overflow

    I am getting started with nuxtjs and vuex. I just encountered an issue accessing a bined injected plugi

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信