We are looking to have a given path display different views depending on whether a user is authenticated, using vue-router
Two approaches I am looking at, but I am not sure how to make work at this point:
- In the
component()
function return one of the two views, though function takes no arguments, so I am not sure how to get the environment context - Have two matching routes, with the first one having the second one as fallback, using logic added to
router.beforeEach()
For the first approach:
{
path: '/mypath/',
component: () => {
if (condition) {
import('src/pages/offers/PublicContent.vue');
} else {
import('src/pages/offers/PrivateContent.vue');
}
}
}
For the second approach:
{
path: '/mypath/',
component: () => {
import('src/pages/offers/PrivateContent.vue');
},
meta: {
requiresAuth: true
}
},
{
path: '/mypath/',
component: () => {
import('src/pages/offers/PublicContent.vue');
}
}
For the second approach I would think the router.beforeEach()
would need to act as if the route didn't match, but I am not sure how to do that in a way that vue-router
tries an alternative route match?