我是Vue的新手,并尝试将'/ home'重定向到'/ travel',但它没有用。
有人能告诉我怎么办?这段代码有什么问题?
谢谢。
<div id="app">
<div>
<router-link to='/home'>Home</router-link>
<router-link to='/news'>News</router-link>
<router-link to='/travel'>Travel</router-link>
</div>
<div>
<router-view></router-view>
</div>
var Home = {
template: '<h3>Home</h3>'
};
var News = {
template: '<h3>News</h3>'
};
var Travel = {
template: '<h3>Travel</h3>'
};
const router = new VueRouter({
routes: [
{ path: '/home', component: Home},
{ path: '/news', component: News},
{ path: '/travel', component:Travel},
//redirect
{ path: '/home', redirect: '/travel' }
]
});
new Vue({
el: '#app',
router,
});
答案 0 :(得分:0)
最简单的方法是为路线添加名称。
{path: '/home',component: Home, name: 'home'}
然后只需使用v-bind:to
:
<router-link v-bind:to="{name: 'home'}">MyLink</router-link>