I'm implementing a auth store(with firebase) and depending on the auth I want to route my user to login/logged page.
I'm basically trying to accomplish this: https://github.com/dannyconnell/vue-composition-api-course/blob/module-23/vue-composition-api-noteballs/src/stores/storeAuth.js
but in typescript.
In my main.ts
, I did declare the store as property:
const app = createApp(App);
const pinia = createPinia();
pinia.use(({ store }) => {
store.router = markRaw(router);
});
app.use(pinia);
app.use(router);
app.mount('#app');
But still, in my store, it doesn't know that I've a router property:
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
}
});
},
//...
}
});
this.router doesn't exist, I get the following error:
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)
So how can I make my store aware that the router
property exists on the created state?
I've read this but I'm not sure what my "router" is considered, and if it's typed, how to indicate when I create the state which store type I declare?
useRouter
fromvue-router
(like shown here)? – Muskratsetup
function only - or any code that it calls, it'll be unusable in most parts of Pinia store – Precipitinrouter
(no useRouter) and use it directly in store actions. As for your case, the most simple way to solve it is to addrouter: null
to the state. Or type your plugin propertly, pinia.vuejs.org/core-concepts/plugins.html#typing-plugins . Any way,this.router
is useless abstraction here – Precipitin