如何在 pinia 商店内使用 vue-router?

J4N*_*J4N 5 typescript vue.js vue-router vuejs3 pinia

我正在实现一个身份验证存储(带有 firebase),并根据身份验证将我的用户路由到登录/记录页面。

我基本上试图完成此任务:https://github.com/dannyconnell/vue-composition-api-course/blob/module-23/vue-composition-api-noteballs/src/stores/storeAuth.js

但在打字稿中。

在我的 中main.ts,我确实将商店声明为财产:

const app = createApp(App);
const pinia = createPinia();
pinia.use(({ store }) => {
  store.router = markRaw(router);
});
app.use(pinia);
app.use(router);
app.mount('#app');
Run Code Online (Sandbox Code Playgroud)

但是,在我的商店中,它仍然不知道我有一个路由器属性:

export const useStoreAuth = defineStore('storeAuth', {
    state: () => {
      return {
        user: {},
      } as AuthState;
    },
    actions: {
      init() {
        onAuthStateChanged(auth, user => {
          if (user && user.uid && user.email) {
            this.user = { id: user.uid, email: user.email };
            this.router.push('/'); //--> router doesn't exists
          } else {
            this.user = null;
            this.router.replace('/auth');//--> router doesn't exists
          }
        });
      },
      //...
    }
});
Run Code Online (Sandbox Code Playgroud)

this.router 不存在,我收到以下错误:

Property 'router' does not exist on type '{ init(): void; registerUser(credentials: any): void; loginUser(credentials: any): void; logoutUser(): void; } & { user: { id: string; email: string; } | null; } & _StoreWithState<"storeAuth", AuthState, {}, { ...; }> & _StoreWithGetters<...> & PiniaCustomProperties<...>'.ts(2339)
Run Code Online (Sandbox Code Playgroud)

那么我怎样才能让我的商店知道该router属性存在于创建状态呢?

我已经读过这篇文章,但我不确定我的“路由器”被认为是什么,如果它是键入的,如何指示当我创建状态时声明哪种存储类型?

小智 7

文档: https: //pinia.vuejs.org/core-concepts/plugins.html#typing-plugins

这是一个完整的例子:

// main.ts
import { createApp, markRaw } from 'vue';
import { createPinia } from 'pinia';
import type { Router } from 'vue-router';
import App from './App.vue';
import router from './router';

declare module 'pinia' {
  export interface PiniaCustomProperties {
    router: Router;
  }
}

const app = createApp(App);
const pinia = createPinia();

pinia.use(({ store }) => {
  store.router = markRaw(router);
});

app.use(pinia);
app.use(router);
app.mount('#app');
Run Code Online (Sandbox Code Playgroud)


Quố*_*ờng -1

你可以试试这个:

import { useRouter } from 'vue-router';

export const useStoreAuth = defineStore('storeAuth', {
    state: () => {
      return {
        user: {},
      } as AuthState;
    },
    actions: {
      init() {
        const router = useRouter(); //--> define router here;
        onAuthStateChanged(auth, user => {
          if (user && user.uid && user.email) {
            this.user = { id: user.uid, email: user.email };
            router.push('/'); //--> this should work
          } else {
            this.user = null;
            router.replace('/auth');//--> this should work
          }
        });
      },
    }
});
Run Code Online (Sandbox Code Playgroud)

如果需要使用的话,在每个action中都定义router可能会有点不方便,但是这是我的使用方式,你可以参考一下。


归档时间:

查看次数:

4579 次

最近记录:

2 年,11 月 前