除了指定路径外,可以自定义名称对路由匹配,举个例子。
<div id="app">
<router-link to="/user/123">user</router-link>
<router-link :to="{name:'users',params:{id:456}}">users</router-link>
<router-view></router-view>
</div>
const user = {
props:['uname','id'],
template:`<h1>user{{uname}}{{id}}</h1>`
}
const users = {
props:['uname','id'],
template:`<h1>users{{uname}}{{id}}</h1>`
}
var vm = new VueRouter({
routes:[
{
path:'/user/:id',
component:user,
props: routes => ({
uname:'小明',
id:routes.params.id
})
},{
path:'/user/:id',
component:users,
name:'users',
props: routes => ({
uname:'小红',
id:routes.params.id
})
}
]
})
var a = new Vue({
el:'#app',
router:vm
})