Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function install (Vue) {
beforeCreate () {
if (isDef(this.$options.router)) {
this._routerRoot = this
this._router = this.$options.router
this._router = typeof this.$options.router === 'function' ? this.$options.router() : this.$options.router
this._router.init(this)
Vue.util.defineReactive(this, '_route', this._router.history.current)
} else {
Expand Down
50 changes: 50 additions & 0 deletions test/unit/specs/factory.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Router from '../../../src/index'
import Vue from 'vue'

describe('router factory', () => {
let InstancedBasedApp, FactoryBasedApp

beforeEach(() => {
const routerFactory = () => new Router({
mode: 'abstract',
routes: [
{
path: '/a',
component: {
name: 'A'
}
}
]
})

const router = routerFactory()

InstancedBasedApp = Vue.extend({
router,
render (h) { return h('div') }
})

FactoryBasedApp = Vue.extend({
router: routerFactory,
render (h) { return h('div') }
})
})

it('should initialize router from factory', () => {
const vm = new FactoryBasedApp()
expect(vm.$router instanceof Router).toBeTruthy()
})

it('should use different router instances on different app instances', () => {
const app = new FactoryBasedApp()
const app2 = new FactoryBasedApp()
expect(app.$router).not.toBe(app2.$router)
})

it('should allow to work as normal', () => {
const app = new InstancedBasedApp()
const app2 = new InstancedBasedApp()
expect(app.$router).toBe(app2.$router)
})
})

11 changes: 6 additions & 5 deletions types/vue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ declare module 'vue/types/vue' {
}
}

declare module 'vue/types/options' {
declare module "vue/types/options" {
type VueRouterFactory = () => VueRouter;
interface ComponentOptions<V extends Vue> {
router?: VueRouter
beforeRouteEnter?: NavigationGuard<V>
beforeRouteLeave?: NavigationGuard<V>
beforeRouteUpdate?: NavigationGuard<V>
router?: VueRouter | VueRouterFactory;
beforeRouteEnter?: NavigationGuard<V>;
beforeRouteLeave?: NavigationGuard<V>;
beforeRouteUpdate?: NavigationGuard<V>;
}
}