When doing the project, encountered the need to do a routing jump, but when the user entered the wrong URL address, or other illegal URL routing address, we might think of jumping to 404 pages. Whether or not you write a 404 page, you need to reassign the page jump when an unmatched route occurs. Perhaps everyone first thought would be the route redirect, redirect to solve this problem. But actually through redirect there is no way to solve this problem better. See Code Red Section
Import vue from ' Vue ' import Router from ' Vue-router '
Import Hello from ' @/components/hello '
Vue.use (Router)
Let routes = [
{
Path: '/',
Name: ' Login ',
Component:login
},
{
Path: '/login ',
Name: ' Login ',
Component:login
},
{
Path: '/index ',
Name: ' Index ',
Component:hello,
}
];
Const ROUTER = new Router ({
History:true,
Routes:routes
});
The focus is as follows: Router.beforeeach (to, from, next) = = {
if (To.matched.length ===0) {//If not matched to route
From.name? Next ({name:from.name}): Next ('/'); If the superior also does not match to the route then the Jump login page, if the superior can match to go to the superior route
} else {
Next (); If the match to the correct jump
}
});
Export Default Router