Resolve vue2.0 route jump mismatch corresponding with route avoid blank page

Source: Internet
Author: User
Tags blank page

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.