-
-
Notifications
You must be signed in to change notification settings - Fork 5k
How can I set a default route on page load #866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi, thanks for filling this issue. Default(fallback) route can already be set with const router = new VueRouter(...)
new Vue({router})
router.replace(...) |
Apologies for commenting on an old issue, but oddly it's not easy to find much in the way of documentation on this. Consider the following code:
Any path typed into the browser ends-up at Ordinarily I would consider this a bug, but since I'm in the early stages of evaluating vuejs, I'm likely missing something obvious. Again, apologies if this is not the appropriate place to bring up this issue. vue 2.8.1 |
@akeating Hi, thank you for your interest in this project. First of all, As for your problem,
router.replace('/foo')
router.replace({name: 'foo', ...}) For further questions, please use gitter or forum or stackoverflow, thanks! |
Huh, I totally misread this thread. Thank you for your prompt reply and clarification. |
IIRC, export default new Router({
routes: [
{
path: '/',
redirect: '/hello-world'
}
]
}) Which will trigger redirect on load. Declarative vs Empiric strikes again. ;) |
What if we want to redirect under some conditions? If we do: const router = new Router({...});
console.log(router.currentRoute.path) We always get This is problematic because if we want to do something like this: router.replace({
path: '/something-else',
query: {
redirect: router.currentRoute.path
}
}); We can't really redirect to the proper path (since the actual path on the browser hasn't been loaded yet). Edit: If we do: const router = new Router({...});
setTimeout(() => {
console.log(router.currentRoute.path);
}, 0); We can see the correct path in the console and not This allows to create conditional redirects and such, but is an ugly solution. |
@PierBover Because, that's a hacky-approach, which is limited. I'm not sure why @fnlctrl used it as an example. Perhaps because it's the shortest way to do a redirect. Anyway, for your case you will need a navigation guard. The docs has it explained. Basically, there's a generic hook that gets called before every route change. In this hook, you can implement your business logic according to where the app should send the user next. You can also cancel the route change. See: https://router.vuejs.org/guide/advanced/navigation-guards.html#navigation-guards |
Thanks @adi518 I know about I was wondering if there was a better approach such as an event when the URL on the address bar of the browser actually becomes the |
the redirection takes a couples of seconds, the best is to edit the manifest.json file to point to / instead of index.html |
How to set default component to the vue-router |
4 years later and I just ran into the exact same issue as you. What I ended up doing, since it's an Electron app and there aren't any instances where you'll run into a 404 issue, is just set my "Home" routes path as a catch-all: export default {
component: () => import('../pages/Index.vue'),
beforeEnter: prefetch,
name: 'index',
path: '*'
} Then in my actual router, I just placed my "Home" route as the last on in the routes array: const router = new VueRouter({
mode: 'history',
base: '/',
routes: [
workflows,
index
]
}) Hopefully this helps out anyone looking into this recently. |
Run into the same issue in Vue 3. I'm doing electron and this is how I fixed it. Since it's Electron I don't need a 404 page. NB: It was the last route in my routes array |
I'm trying to use
vue-router
withelectron.js
to make a desktop app, I currently have these routesCurrently when the program loads,
window.location.pathname
is something like/Users/name/appname/app/views/index.html'
which doesn't match ANY of my paths, so I use apath: '*', redirect: '/'
to send it to the home page on initial load.I feel like there should be a better way to set a default route on initial load, is there nothing like
<router-view default="{name: 'home'}"></router-view>
or somethingThe text was updated successfully, but these errors were encountered: