vue路由----404页面单独跳转
定义404错误模版并配置路径import Vue from 'vue'import VueRouter from 'vue-router'Vue.use(VueRouter)const Home = {template:`<div><h2>Home</h2><p>this is Home</...
·
注意:
配置404页面一定要在路由表的最后一个,放在*的下面路由是不起作用的
定义404错误模版并配置路径
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const Home = {
template:`
<div>
<h2>Home</h2>
<p>this is Home</p>
</div>
`
}
const Parent = {
template:`
<div>
<h2>Parent</h2>
<p>this is Parent</p>
</div>
`
}
const Page404 = { /*404页面默认跳转,定义时不能以数字开头*/
template:`
<div>
<h2>404</h2>
<p>error:404 页面走丢了</p>
</div>
`
}
const router = new VueRouter({
mode:'history',
data(){
return{
aaa:'fade'
}
},
base: __dirname,
routes:[
{path:'/',component:Home},
{path:'/Parent',component:Parent},
{path:'*',component:Page404}
]
})
new Vue({
router,
template:`
<div id="app">
<h1>this is transition</h1>
<ul>
<li><router-link to="/">/</router-link></li>
<li><router-link to="/Parent">/Parent</router-link></li>
<li><router-link to="/asdf">/错误的路由链接,跳转至404页面</router-link></li>
</ul>
<transition :name="aaa" mode="out-in">
<router-view></router-view>
</transition>
</div>
`,
watch:{
'$route' (to,from){
console.log(to);
console.log(from);
if(from.path == '/Parent'){
this.aaa = 'fade1';
}else{
this.aaa = 'fade2';
}
}
}
}).$mount('#app')
在之后路径出错后便会跳转到这里
运行结果:
更多推荐
已为社区贡献5条内容
所有评论(0)