Skip to content

Commit a80f16c

Browse files
committed
fix navigation without instance + support location arg in getMatchedComponents (fix #922)
1 parent a95795c commit a80f16c

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

src/history/base.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ export class History {
9292
if (this.pending === route) {
9393
this.pending = null
9494
cb(route)
95-
this.router.app.$nextTick(() => {
96-
postEnterCbs.forEach(cb => cb())
97-
})
95+
if (this.router.app) {
96+
this.router.app.$nextTick(() => {
97+
postEnterCbs.forEach(cb => cb())
98+
})
99+
}
98100
}
99101
})
100102
})

src/index.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,29 @@ export default class VueRouter {
108108
this.go(1)
109109
}
110110

111-
getMatchedComponents (): Array<any> {
112-
if (!this.currentRoute) {
111+
getMatchedComponents (to?: RawLocation): Array<any> {
112+
const route = to
113+
? this.resolve(to).resolved
114+
: this.currentRoute
115+
if (!route) {
113116
return []
114117
}
115-
return [].concat.apply([], this.currentRoute.matched.map(m => {
118+
return [].concat.apply([], route.matched.map(m => {
116119
return Object.keys(m.components).map(key => {
117120
return m.components[key]
118121
})
119122
}))
120123
}
121124

122-
resolve (to: RawLocation, current?: Route, append?: boolean): {href: string} {
125+
resolve (
126+
to: RawLocation,
127+
current?: Route,
128+
append?: boolean
129+
): {
130+
normalizedTo: Location,
131+
resolved: Route,
132+
href: string
133+
} {
123134
const normalizedTo = normalizeLocation(to, current || this.history.current, append)
124135
const resolved = this.match(normalizedTo, current)
125136
const fullPath = resolved.redirectedFrom || resolved.fullPath

test/unit/specs/node.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import Vue from 'vue'
2+
import VueRouter from '../../../src/index'
3+
4+
Vue.use(VueRouter)
5+
6+
describe('Usage in Node', () => {
7+
it('should be in abstract mode', () => {
8+
const router = new VueRouter()
9+
expect(router.mode).toBe('abstract')
10+
})
11+
12+
it('should be able to navigate without app instance', () => {
13+
const router = new VueRouter({
14+
routes: [
15+
{ path: '/', component: { name: 'foo' }},
16+
{ path: '/bar', component: { name: 'bar' }}
17+
]
18+
})
19+
router.push('/bar')
20+
expect(router.history.current.path).toBe('/bar')
21+
})
22+
23+
it('getMatchedComponents', () => {
24+
const Foo = { name: 'foo' }
25+
const Bar = { name: 'bar' }
26+
const Baz = { name: 'baz' }
27+
const router = new VueRouter({
28+
routes: [
29+
{ path: '/', component: Foo },
30+
{ path: '/bar', component: Bar, children: [
31+
{ path: 'baz', component: Baz }
32+
]}
33+
]
34+
})
35+
expect(router.getMatchedComponents('/')).toEqual([Foo])
36+
expect(router.getMatchedComponents('/bar/baz')).toEqual([Bar, Baz])
37+
})
38+
})

0 commit comments

Comments
 (0)