HMR not working when using store in router beforeEach #1043
Replies: 4 comments 10 replies
-
I fixed that error. I had circular dependencies, accountStore in router and router in accountStore. Looks like problem with vite since in webpack there was no such problem. |
Beta Was this translation helpful? Give feedback.
-
I don't think I have circular dependency. But I do have lazy route for this component. If I refresh the page, it works fine, but as soon as I make any change in Lesson component and save, I get this error. |
Beta Was this translation helpful? Give feedback.
-
@tciochsolv The issue got solved. Somehow, I think the error was misleading. The real issue in my code was that I was importing i18n instance from main.ts file inside onMounted() hook. So, I changed it to below code and it is working fine. const { locale } = useI18n({ useScope: "global" }); |
Beta Was this translation helpful? Give feedback.
-
Hi all, so i have something similar, when i edit a .ts file HMR works fine, only when editing a .Vue file I'm getting the error router.beforeEach(async (to, _from) => {
// TODO: firure out how to fix this change on TS files goes fine
// but a change to a .Vue file triggers: "ReferenceError: Cannot access 'useSessionStore' before initialization"
const store = useSessionStore();
if (!store.signedIn && to.name !== "login") {
// redirect the user to the login page
return { name: "login" };
}
}); Obviously when reloading the page or deploying everything works as expected. Im using the default Vite setup as described on the VueJs site. import { resolve } from "path";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
// https://vitejs.dev/config/
export default defineConfig(({ command }: any) => {
const isDev = command !== "build";
if (isDev) {
// Terminate the watcher when Phoenix quits
process.stdin.on("close", () => {
process.exit(0);
});
process.stdin.resume();
}
return {
plugins: [
vue()
],
server: {
host: "0.0.0.0",
},
resolve: {
alias: {
"@": resolve(__dirname, "src"),
},
},
build: {
target: "esnext", // build for recent browsers
outDir: "../priv/static", // emit assets to priv/static
emptyOutDir: true,
sourcemap: true, // enable source map in dev build
manifest: false, // do not generate manifest.json
rollupOptions: {
input: {
main: "./src/main.ts",
},
output: {
entryFileNames: "assets/[name].js", // remove hash
chunkFileNames: "assets/[name].js",
assetFileNames: "assets/[name][extname]",
},
},
},
};
}); Thanks |
Beta Was this translation helpful? Give feedback.
-
Reproduction
Vue 3.2.30, vite 2.8.0, vue-router 4.0.12, pinia 2.0.11
I have beforeEach guard where I have method from file authorization.ts
like this:
And in authorization.ts I have:
And when I'm editing something in component that is directly assigned to route component, hot reload doesn't work.
It works when I'm editing some things in child components or App.vue for example.
The errors I get in console when editing LoginView is:
ReferenceError: can't access lexical declaration 'useAccountStore' before initialization
[hmr] Failed to reload /src/Account/views/Login/LoginView.vue. This could be due to syntax errors or importing non-existent modules. (see errors above)
Uncaught (in promise) TypeError: (destructured parameter) is undefined
When I don't use pinia stores in before each, everything works.
My setup: piniaInstall.ts
main.ts
I made it by piniaInstall because there was error that pinia was accessed before installation.
Steps to reproduce the behavior
When refreshing page, everything works.
Expected behavior
HMR should work, no errors
Beta Was this translation helpful? Give feedback.
All reactions