为什么我有错误的注入“商店”当我尝试`this.store.commit()`时找不到。

发布于 2025-02-08 05:28:03 字数 1572 浏览 1 评论 0原文

我正在尝试使用Vuex存储值。我正在使用Vuex 4.0而不是3.0。

因此,我正在尝试提交这样的值,就像我曾经在Vuex 3中所做的那样:

...
 this.$store.commit("changerusername", u)
...

我尝试了以下内容:

在我的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");

sote/store/store内部。

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;
    }
  },
});

<template>
  <router-view />
</template>
<script>
import { defineComponent } from "vue";

export default defineComponent({
  name: "app",
});
</script>

import { useQuasar } from "quasar";
import { defineComponent } from "vue";
import { ref } from "vue";
import axios from "axios";
import { useStore } from "vuex";


    setup() {
      const store = useStore();

      return {
        store,
      };
    },

​这样:


      loginArrow: function (u, p) {
        this.store.commit("changeusername", u);
      },

但是当我这样做时,我会收到此错误:注入“存储”找不到。

我做错了什么?

I'm trying to store values using VueX. I'm using Vuex 4.0 not the 3.0.

So I'm trying to commit a value like this, as I was used to in Vuex 3 :

...
 this.$store.commit("changerusername", u)
...

So I tried this :

Inside my main.js I have this :

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 :

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 :

<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..

What I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦途 2025-02-15 05:28:03

如果您使用的是组成API,则可以将loginarrow函数更改为这样的东西,因为关键字在构图API中不起作用:

const loginArrow = (u, p) => {
    store.commit("changeusername", u);
}

您也可以尝试使用store.value.commit(...)而不是store.commit(...)

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:

const loginArrow = (u, p) => {
    store.commit("changeusername", u);
}

You can also try to use store.value.commit(...) instead of store.commit(...)

树深时见影 2025-02-15 05:28:03

我发现原因不起作用的原因。它不是在功能中,而是直接在类星体中。

在文件夹应用程序/.quasar中,有4个文件app.js,client-entry.js,client-prefetch.js和quasar-user-options.js。

通常,它会自动配置,但是iDk为什么不可用,所以我通过与另一个项目进行比较来手动进行操作。

谢谢您的帮助!

I found the reason why that wasn't working. it wasn't in the function but in quasar directly.

In the folder app/.quasar there's 4 files app.js, client-entry.js, client-prefetch.js and quasar-user-options.js.

Normally it configure automatically but idk why quasar didn't so I did it manually by comparing with another project.

Thank you for the help !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文