admin管理员组文章数量:1422260
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 badges2 Answers
Reset to default 3You 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
本文标签: javascriptAccessing Nuxt plugin function in vuex getterStack Overflow
版权声明:本文标题:javascript - Accessing Nuxt plugin function in vuex getter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745362965a2655380.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论