简体   繁体   English

Vue.js Pinia 没有安装?

[英]Vue.js Pinia not installed?

I am constantly getting this error message:我不断收到此错误消息:

Uncaught Error: [🍍]: getActivePinia was called with no active Pinia. Did you forget to install pinia?
    const pinia = createPinia()
    app.use(pinia)
This will fail in production.
    at useStore (pinia.mjs?b318:1692:1)
    at eval (App.vue?91a0:77:1)
    at ./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/App.vue?vue&type=script&lang=js (app.js:19:1)
    at __webpack_require__ (app.js:386:33)
    at fn (app.js:642:21)
    at eval (App.vue?vue&type=script&lang=js:5:206)
    at ./src/App.vue?vue&type=script&lang=js (app.js:195:1)
    at __webpack_require__ (app.js:386:33)
    at fn (app.js:642:21)
    at eval (App.vue:3:90)

This is my store.js file这是我的 store.js 文件

import { defineStore } from "pinia";

export const useUserStore = defineStore("user", {
  state: () => {
    return {
      user: null,
    };
  },
  actions: {
    setUser(username) {
      this.user = username;
    },
  },
});

In main.js I have在 main.js 我有

import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import { createPinia } from "pinia";

const pinia = createPinia();
createApp(App).use(router).use(pinia).mount("#app");

and this is the App.vue file这是 App.vue 文件

import { auth } from "@/firebase";
import { onAuthStateChanged } from "@firebase/auth";
import { useUserStore } from "@/stores/store";

const store = useUserStore();

export default {
  // setup() {
  //   const store = useUserStore();

  //   return {
  //     // you can return the whole store instance to use it in the template
  //     store,
  //   };
  // },
  name: "app",
  data() {
    return {
      store,
    };
  },
  methods: {
    signOut() {
      auth.signOut().then(() => {
        this.$router.push({ name: "login" });
      });
    },
  },
};
onAuthStateChanged(auth, (user) => {
  if (user) {
    console.log(user.email);
    store.setUser(user.email);
  } else {
    console.log("no user");
    store.setUser(null);
  }
});

It worked and then Imported Bootstrap, then I got the error msg in the beginning.它起作用了,然后导入了引导程序,然后我一开始就收到了错误消息。

I removed the bootstrap and it still doesn't work.我删除了引导程序,但它仍然不起作用。

Any ideas?有任何想法吗?

Pinia is installed via npm. Pinia 通过 npm 安装。

You are calling pinia outside of your component setup您在组件设置之外调用pinia

This is not correct这是不正确的

const store = useUserStore();

export default {
  // setup() {
  //   const store = useUserStore();

  //   return {
  //     // you can return the whole store instance to use it in the template
  //     store,
  //   };
  // },

useUserStore should be inside your setup method. useUserStore应该在您的setup方法中。

correct正确的

export default {
   setup() {
     const store = useUserStore();
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM