I have setup a route
in vue-router 4
that should load a component
dynamically depending on whether the user is logged in or not. I did it like this (there may be a better way?):
import Personal from '../views/Personal.vue';
import Public from '../views/Public.vue';
routes: [
{
path: '/',
component: async () => {
const isLoggedIn = await authenticateUser();
if (isLoggedIn == true) {
return Personal
} else {
return Public
}
}
}
]
The App.vue
file is this:
<template>
<div id="app">
<Site-Header></Site-Header>
<router-view></router-view>
<Site-Footer></Site-Footer>
</div>
</template>
The problem is that if a user logs in from the homepage route with path of '/'
, he doesn't navigate away from this route. Instead I would like vue-router
to just load the Personal
component instead.
The switch between Personal
and Public
only seems to work if I hard refresh the page, otherwise no changes happen. So if a user logs in, they still see the Public.vue
component, then after a page refresh they see the Personal.vue
component. If they then logout, they still see the Personal.vue
component until they refresh the page.
How could I force vue-router to analyse the route after log-in/log-out and load the correct component?