File tree Expand file tree Collapse file tree 3 files changed +58
-7
lines changed Expand file tree Collapse file tree 3 files changed +58
-7
lines changed Original file line number Diff line number Diff 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 } )
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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+ } )
You can’t perform that action at this time.
0 commit comments