最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
解决vue2.0路由跳转未匹配相应用路由避免出现空白页面的问题
时间:2022-06-25 15:43:18 编辑:袖梨 来源:一聚教程网
在做项目的时候,遇到需要做路由跳转,但当用户输入错误url地址,或是其它非法url路由地址,我们或许会想到跳转至404页面。不管你有没有写一个404页面,当出现未匹配路由都需重新指定页面跳转。可能大家首先想到会是路由重定向,redirect来解决这个问题。但实际上通过redirect是没办法更好解决这个问题的。
看代码红色部分
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 });
重点如下:
router.beforeEach((to, from, next) => {
if (to.matched.length ===0) { //如果未匹配到路由
from.name ? next({ name:from.name }) : next('/'); //如果上级也未匹配到路由则跳转登录页面,如果上级能匹配到则转上级路由
} else {
next(); //如果匹配到正确跳转
}
});
相关文章
- Binance.US已上线VIRTUAL 04-30
- binance官网电脑下载_binance苹果版下载地址V2.57.7 04-30
- 比特币交易量最大的交易所是哪个?全球最大的比特币交易平台排名 04-30
- 奥比岛梦想国度卡死闪退有哪些解决方法 04-30
- DNF手游骨戒在哪个位置 04-30
- 回望羊驼:当利空成为短暂的财富密码 04-30