[英]Why do I have the error `injection "store" not found` when I try `this.store.commit()`?
I'm trying to store values using VueX.我正在尝试使用 VueX 存储值。 I'm using Vuex 4.0 not the 3.0.
我使用的是 Vuex 4.0 而不是 3.0。
So I'm trying to commit a value like this, as I was used to in Vuex 3:所以我正在尝试提交这样的值,就像我在 Vuex 3 中习惯的那样:
...
this.$store.commit("changerusername", u)
...
So I tried this:所以我尝试了这个:
Inside my main.js
I have this:在我的
main.js
里面我有这个:
import { createApp } from "vue";
import { store } from "./store";
import App from "./App.vue";
const app = createApp(App);
app.use(store);
app.mount("#app");
Inside my store/store.js
, I have this:在我的
store/store.js
,我有这个:
import vue from "vue";
import { createStore } from "vuex";
export const store = createStore({
state: {
username: null,
token: null,
},
getters: {
getUsername: function (state) {
return `${state.username}`;
},
},
mutation: {
changeusername: function (state, newusername) {
state.username = newusername;
}
},
});
App.vue
have this: App.vue
有这个:
<template>
<router-view />
</template>
<script>
import { defineComponent } from "vue";
export default defineComponent({
name: "app",
});
</script>
Ok now, inside the file where I want to do my commit, I wrote this:现在好了,在我想要提交的文件中,我写了这个:
Imports:进口:
import { useQuasar } from "quasar";
import { defineComponent } from "vue";
import { ref } from "vue";
import axios from "axios";
import { useStore } from "vuex";
The setup:设置:
setup() {
const store = useStore();
return {
store,
};
},
And I use it like this:我这样使用它:
loginArrow: function (u, p) {
this.store.commit("changeusername", u);
},
But when I do this I got this error: injection "store" not found.
但是当我这样做时,我得到了这个错误:
injection "store" not found.
. .
What I am doing wrong?我做错了什么?
If you're using the Composition API you could change your loginArrow
function to something like this because the keyword this
does not work in the Composition API:如果您使用的是组合 API,您可以将
loginArrow
function 更改为类似这样的内容,因为关键字this
在组合 API 中不起作用:
const loginArrow = (u, p) => {
store.commit("changeusername", u);
}
You can also try to use store.value.commit(...)
instead of store.commit(...)
您也可以尝试使用
store.value.commit(...)
而不是 store.commit( store.commit(...)
I found the reason why that wasn't working.我找到了那行不通的原因。 it wasn't in the function but in quasar directly.
它不在 function 中,而是直接在类星体中。
In the folder app/.quasar there's 4 files app.js, client-entry.js, client-prefetch.js and quasar-user-options.js.在文件夹 app/.quasar 中有 4 个文件 app.js、client-entry.js、client-prefetch.js 和 quasar-user-options.js。
Normally it configure automatically but idk why quasar didn't so I did it manually by comparing with another project.通常它会自动配置,但我不知道为什么 quasar 没有,所以我通过与另一个项目进行比较手动完成了它。
Thank you for the help !感谢您的帮助 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.