@@ -33,7 +33,7 @@ const Baz = {
33
33
} ,
34
34
template : `
35
35
<div>
36
- <p>baz ({{ saved ? 'saved' : 'not saved' }})<p>
36
+ <p>baz ({{ saved ? 'saved' : 'not saved' }})</ p>
37
37
<button @click="saved = true">save</button>
38
38
</div>
39
39
` ,
@@ -46,7 +46,7 @@ const Baz = {
46
46
}
47
47
}
48
48
49
- // Baz implements an in-component beforeRouteEnter hook
49
+ // Qux implements an in-component beforeRouteEnter hook
50
50
const Qux = {
51
51
data ( ) {
52
52
return {
@@ -87,6 +87,57 @@ const Quux = {
87
87
}
88
88
}
89
89
90
+ // Nested implements an in-component beforeRouteUpdate hook that uses
91
+ // next(vm => {})
92
+ const Nested = {
93
+ data ( ) {
94
+ return {
95
+ calls : 0 ,
96
+ updates : 0
97
+ }
98
+ } ,
99
+ template : `
100
+ <div>
101
+ <p>
102
+ nested calls:{{ calls }} updates:{{ updates }}
103
+ </p>
104
+ <router-view></router-view>
105
+ </div>
106
+ ` ,
107
+ beforeRouteUpdate ( to , from , next ) {
108
+ // calls is incremented every time anything navigates, even if the
109
+ // navigation is canceled by a child component.
110
+ this . calls ++
111
+
112
+ next ( vm => {
113
+ // updates is only incremented once all children confirm and complete
114
+ // navigation. If any children load data async, then this will not be
115
+ // called until all children have called next() themselves. If any child
116
+ // cancels navigation, then this will never be called.
117
+ vm . updates ++
118
+ } )
119
+ }
120
+ }
121
+
122
+ // Buux implements a cancelable beforeRouteUpdate hook
123
+ const Buux = {
124
+ data ( ) {
125
+ return {
126
+ prevId : 0
127
+ }
128
+ } ,
129
+ template : `<div>buux id:{{ $route.params.id }} prevId:{{ prevId }}</div>` ,
130
+ beforeRouteUpdate ( to , from , next ) {
131
+ if ( window . confirm ( `Navigate to ${ to . path } ?` ) ) {
132
+ next ( vm => {
133
+ vm . prevId = from . params . id
134
+ } )
135
+ } else {
136
+ next ( false )
137
+ }
138
+ }
139
+ }
140
+
90
141
const router = new VueRouter ( {
91
142
mode : 'history' ,
92
143
base : __dirname ,
@@ -114,7 +165,14 @@ const router = new VueRouter({
114
165
} } ,
115
166
116
167
// in-component beforeRouteUpdate hook
117
- { path : '/quux/:id' , component : Quux }
168
+ { path : '/quux/:id' , component : Quux } ,
169
+ { path : '/nested' , component : Nested ,
170
+ children : [
171
+ { path : '' , component : Home } ,
172
+ { path : 'qux' , component : Qux } ,
173
+ { path : 'buux/:id' , component : Buux }
174
+ ]
175
+ }
118
176
]
119
177
} )
120
178
@@ -140,6 +198,10 @@ new Vue({
140
198
<li><router-link to="/qux-async">/qux-async</router-link></li>
141
199
<li><router-link to="/quux/1">/quux/1</router-link></li>
142
200
<li><router-link to="/quux/2">/quux/2</router-link></li>
201
+ <li><router-link to="/nested">/nested</router-link></li>
202
+ <li><router-link to="/nested/qux">/nested/qux</router-link></li>
203
+ <li><router-link to="/nested/buux/1">/nested/buux/1</router-link></li>
204
+ <li><router-link to="/nested/buux/2">/nested/buux/2</router-link></li>
143
205
</ul>
144
206
<router-view class="view"></router-view>
145
207
</div>
0 commit comments