由于某种原因,常规重定向规则不适用于历史记录模式,但适用于哈希模式。
以下是代码段:
let first = {
name: "first",
template: `<section><h1>First</h1></section>`
};
let second = {
name: "second",
template: `<section><h2>Second</h2></section>`
};
new Vue({
el: "#app",
router: new VueRouter(
{
mode: "history",
routes: [
{ path: "/", redirect: { name: 'first' }}, // Works with hash mode, but does't with history
{ path: "/first", component: first, name: "first"},
{ path: "/second", component: second, name: "second" }
]
}
)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.10/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-router/3.1.3/vue-router.min.js"></script>
<div id="app">
<router-link to="/first">First</router-link>
<router-link to="/second">Second</router-link>
<router-view></router-view>
</div>
为什么?