导航守卫。404页面设置
<h1>全局导航守卫:(在router.js中添加)</h1><div class="cnblogs_code">
<pre>router.beforeEach((to,from,next)=<span style="color: rgba(0, 0, 0, 1)">>{
if(to.name!</span>==<span style="color: rgba(0, 0, 0, 1)">'login'){
if(localStorage.getItem('userid')){
next()
}else{
next('login')
}
}else{
next()
}
})</span></pre>
</div>
<h1>组件内导航守卫:(在组件内添加)</h1>
<div class="cnblogs_code">
<pre>beforeRouteEnter(to,form,<span style="color: rgba(0, 0, 0, 1)">next){
if(localStorage.getItem('userid')){
next()
}else{
alert('请先登录')
next('login')
}
}</span>,</pre>
</div>
<h1>404页面设置:(在router.js中设置)</h1>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">{ //输错路由重定向到登陆页面
path: </span>"*",<span style="color: rgba(0, 0, 0, 1)">
redirect: </span>"404"<span style="color: rgba(0, 0, 0, 1)">
}</span></pre>
</div>
</div>
<div id="MySignature" role="contentinfo">
长风破浪会有时,直挂云帆济沧海<br><br>
来源:https://www.cnblogs.com/never404/p/12196386.html
頁:
[1]