1
1
import Navigator from './components/Navigator'
2
2
3
3
export default function install ( Vue , { routes} ) {
4
+ let appRoot ;
5
+ const start = Vue . prototype . $start
6
+ Vue . prototype . $start = function ( ) {
7
+ appRoot = this
8
+ start . call ( this )
9
+ }
4
10
Vue . component ( 'Navigator' , Navigator )
5
11
6
12
Object . keys ( routes ) . map ( path => {
@@ -20,24 +26,29 @@ export default function install(Vue, {routes}) {
20
26
data : {
21
27
path : false ,
22
28
paths : { } ,
23
- defaultPath : '/'
29
+ defaultPaths : { } ,
24
30
} ,
25
31
computed : {
26
32
route ( ) {
27
- return routes [ this . path || this . defaultPath ]
33
+ return this . routes ( 'navigator' )
34
+ } ,
35
+ routes ( ) {
36
+ return id => routes [ this . paths [ id ] || this . defaultPaths [ id ] ]
28
37
} ,
29
38
} ,
30
39
methods : {
31
- _resolveComponent ( defaultPath ) {
32
- if ( defaultPath ) this . defaultPath = defaultPath
40
+ _resolveComponent ( defaultPath , id ) {
41
+ if ( defaultPath ) {
42
+ this . $set ( this . defaultPaths , id , defaultPath )
43
+ }
33
44
34
- if ( this . route ) {
35
- return this . route . component
45
+ if ( this . routes ( id ) ) {
46
+ return this . routes ( id ) . component
36
47
}
37
48
return false
38
49
} ,
39
50
_updatePath ( path , id = 'navigator' ) {
40
- if ( id === 'navigator' ) {
51
+ if ( id === 'navigator' ) {
41
52
this . path = path
42
53
}
43
54
this . $set ( this . paths , id , path )
@@ -48,17 +59,18 @@ export default function install(Vue, {routes}) {
48
59
49
60
if ( ! matchedRoute ) {
50
61
if ( TNS_ENV === 'development' ) {
51
- throw new Error ( `Navigating to a route that does not exist: ${ to } ` )
62
+ throw new Error ( `[Navigator] Navigating to a route that does not exist: ${ to } ` )
52
63
}
53
64
return false
54
65
}
55
66
56
- options = Object . assign ( { frame : 'navigator' } , options )
67
+ options = Object . assign ( { frame : 'navigator' } , options )
57
68
58
69
return this . $navigateTo ( matchedRoute . component , options )
70
+ . catch ( err => console . log ( `[Navigator] Failed to navigate: ${ err } ` ) )
59
71
} ,
60
72
back ( options , ...args ) {
61
- options = Object . assign ( { frame : 'navigator' } , options )
73
+ options = Object . assign ( { frame : 'navigator' } , options )
62
74
return this . $navigateBack . call ( this , options , ...args )
63
75
}
64
76
} ,
0 commit comments