简体   繁体   English

为什么当我尝试 `this.store.commit()` 时出现错误 `injection "store" not found`?

[英]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.

相关问题 为什么我尝试在 mui 图标中多次导入图标时出现错误? - Why do I have an error when I try a multiple import of icons in mui icons? 当我尝试存储变量时不起作用? - When I try and store a variable it does not work? 当第一次将数据存储在 localstorage 中时,它会被存储,但第二次我尝试存储时,它显示错误 push is not function 为什么? - When stored data in localstorage first time it will be stored but second time I try to store then it display error push is not function why? 当我尝试执行弹出窗口时,为什么抛出页面找不到错误? - When I try to execute popup, why is it throwing page not found error? 当我尝试从 vuex 商店调度动作时,Nuxt 抛出错误“未知动作类型” - Nuxt is throwing the error 'unknown action type' when I try to dispatch action from vuex store 如果通过 client.html 将数据存储在 server.py 中,它说我成功了,但是当我尝试检索它时,它说 404 not found - If store data in server.py through client.html it says that I did it successfully, but when I try to retrieve it, it says 404 not found 当我尝试存储 select 值时,setState 不起作用 - setState is not working when i try to store select value 为什么在将Javascript对象推送到数组之前必须将其存储在变量中? - Why do I have to store this Javascript object in a variable before pushing it to an array? 如何存储和检索自己制作的JavaScript变量? - How do I store and retrieve javascript variables that I have made? 我尝试在商店中恢复 state (react, redux) - I try to recover a state in store (react, redux)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM