Can not use store in router.beforeEach #723
-
Reproductionvue: 2.6.14 // app.ts
import router from '@/router'
function initApp() {
Vue.use(VueCompositionAPI);
Vue.use(PiniaVuePlugin);
const pinia = createPinia();
const app = new Vue({
pinia,
router,
i18nIntl,
render: (h: CreateElement) => h(App),
});
app.$mount('#app');
} export function createRouter() {
// init
let router: VueRouter = new VueRouter({
routes: [{
path: '/',
component: () => import('@/views/index/index.vue')
}], //
});
router.beforeEach((to, from, next) => {
const axiosStore = useAxiosStore();
const authStore = useAuthStore();
});
return router;
}
export default createRouter(); Expected behaviorI could use store as document Actual behaviorI got an error
if i remove it from router.beforeEach, it works with vue files. Additional informationI notice and page was render |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 14 replies
-
pinia was use getCurrentInstance( I had tried use getCurrentInstance in router.breforeEach and got |
Beta Was this translation helpful? Give feedback.
-
also, i want to use like this when define routes in vue-router , it's also failed
|
Beta Was this translation helpful? Give feedback.
-
I really want to use pinia store inside beforeEach. Is there any solution? |
Beta Was this translation helpful? Give feedback.
-
I'm pretty sure that the problem you're running into is that when you export the function as
The function will run on import and since this happens before the Pinia instance has been created it won't work. The second example o n the "Stores outside of components"-section have an example of a structure that might work better https://pinia.esm.dev/core-concepts/outside-component-usage.html#single-page-applications |
Beta Was this translation helpful? Give feedback.
-
I met the same issue on a Vue 2 project with @vue/cli and vue-router v3 setup, and a The solution for me has been to call import Vue from 'vue'
import { createPinia, PiniaVuePlugin } from 'pinia'
import Router from "vue-router";
import router from "./router"; // <--- export default new Router(...)
import App from './App.vue'
Vue.use(PiniaVuePlugin)
const pinia = createPinia()
Vue.use(Router) // <--- MUST BE AFTER Vue.use(PiniaVuePlugin)
new Vue({
render: h => h(App),
pinia,
router
}).$mount('#app') |
Beta Was this translation helpful? Give feedback.
-
I had this issue as well with Vue 2. The problem seems to be the way Vue is built in Vue 2 compared to vue 3, so the router will be evalutated before the pinia store has been added to the instance. I solved it by allowing the pinia store to be exported from main.js: //main.js then in router index.js I imported this along with the store I needed:
then used it in the routerguard like this:
And I could use the store with no issues. |
Beta Was this translation helpful? Give feedback.
I'm pretty sure that the problem you're running into is that when you export the function as
export default createRouter();
The function will run on import and since this happens before the Pinia instance has been created it won't work. The second example o n the "Stores outside of components"-section have an example of a structure that might work better
https://pinia.esm.dev/core-concepts/outside-component-usage.html#single-page-applications