首页 > 解决方案 > Vue-router 在运行时改变模式

问题描述

我使用选项实例化 vue-router:

mode: "abstract"

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

this.$router.mode = 'hash';

然后我导航:

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

URL 中没有散列。

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

标签: vue.jsvue-router

解决方案


更改模式不会做任何事情,而模式仅在 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


推荐阅读