2017-10-05 71 views

回答

2

在路由列表末尾使用catch all路由。

const routes = [ 
    { 
     path:'/', 
     component:Home, 
    }, 
    { 
     path: "*", 
     component: NotFound 
    } 
] 

這裏是一個例子。

console.clear() 
 

 
const Foo = { template: '<div>foo</div>' } 
 
const NotFound = { template: '<div>404 page</div>' } 
 

 
const routes = [ 
 
    { path: '/foo', component: Foo }, 
 
    { path: '*', component: NotFound } 
 
] 
 

 
const router = new VueRouter({ 
 
    routes 
 
}) 
 

 
const app = new Vue({ 
 
    router 
 
}).$mount('#app')
<script src="https://unpkg.com/vue"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-router/2.7.0/vue-router.js"></script> 
 

 
<div id="app"> 
 
    <router-view></router-view> 
 
</div>

+0

哦啦啦!你搖滾!這樣可行 – dexter00x