运行时 Vue 路由器更改模式



我使用选项实例化vue路由器:

mode: "abstract"

之后,我想在运行时更改路由:

this.$router.mode = 'hash';

然后我导航:

this.$router.push({name: 'test'});

并且 URL 中没有哈希。

那么如何在运行时更改模式呢?

更改模式不会执行任何操作,而该模式仅在 VueRouter 的构造函数中使用。在模式下,VueRouter 执行以下代码 (https://github.com/vuejs/vue-router/blob/dev/src/index.js(:

switch (mode) {
case 'history':
this.history = new HTML5History(this, options.base)
break
case 'hash':
this.history = new HashHistory(this, options.base, this.fallback)
break
case 'abstract':
this.history = new AbstractHistory(this, options.base)
break
default:
if (process.env.NODE_ENV !== 'production') {
assert(false, `invalid mode: ${mode}`)
}
}

但基本上最好重新创建你的 VueRouter

那么,为什么不使用mode: "hash"启动它呢?

最新更新