【发布时间】:2020-02-22 01:56:40
【问题描述】:
有一个应用程序,其中路由由两侧处理:一些路由由 CMS 处理,一些由 vue-router 处理。例如:http://localhost/ 由 CMS 和网络服务器 处理,但 http://localhost/complex-page/#/my-route 由 CMS 和 Vue 路由器 处理。
这意味着它有两个vue-router 实例:全局(仅用于$route 全局,它使用history 模式)和本地(用于与hash 模式一起使用)。这里有一个问题:本地路由器表现得好像他覆盖了全局模式,并且散列添加到各处的路由,即使没有具有本地路由的组件。
这是非常接近实际项目的代码:
let first = {
name: "first",
template: `<section><h1>First</h1></section>`
};
let second = {
name: "second",
template: `<section><h2>Second</h2></section>`
};
let outerComponent = Vue.component('container', {
template: `
<section>
<h2>Outer</h2>
<h3>Path: {{window.location.href}}</h3>
<nav>
<router-link to='/first' append>First</router-link>
<router-link to='/second' append>Second</router-link>
</nav>
<h3>Inner:</h3>
<router-view></router-view>
</section>
`,
router: new VueRouter({
mode: 'hash', // HASH!!!
components: {
'first': first,
'second': second
},
routes: [{
path: "/first",
component: first,
name: "first"
},
{
path: "/second",
component: second,
name: "second"
}
]
})
});
// Root!
new Vue({
el: "#app",
router: new VueRouter({
mode: "history" // HISTORY!
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-router/3.1.3/vue-router.min.js"></script>
<div id="app">
<!-- Even if delete this node, hash will be added -->
<container></container>
</div>
如何全局禁用路径的哈希并将其添加到某些路由中?
【问题讨论】:
标签: vuejs2 vue-router