@@ -135,12 +135,30 @@ export const RouterViewImpl = /*#__PURE__*/ defineComponent({
135
135
{ flush : 'post' }
136
136
)
137
137
138
+ let matchedRoute : RouteLocationMatched | undefined
139
+ let currentName : string
140
+ // Since in Vue the entering view mounts first and then the leaving unmounts,
141
+ // we need to keep track of the last route in order to use it in the unmounted
142
+ // event
143
+ let lastMatchedRoute : RouteLocationMatched | undefined
144
+ let lastCurrentName : string
145
+
146
+ const onVnodeUnmounted : VNodeProps [ 'onVnodeUnmounted' ] = vnode => {
147
+ // remove the instance reference to prevent leak
148
+ if ( lastMatchedRoute && vnode . component ! . isUnmounted ) {
149
+ lastMatchedRoute . instances [ lastCurrentName ] = null
150
+ }
151
+ }
152
+
138
153
return ( ) => {
139
154
const route = routeToDisplay . value
155
+ lastMatchedRoute = matchedRoute
156
+ lastCurrentName = currentName
140
157
// we need the value at the time we render because when we unmount, we
141
158
// navigated to a different location so the value is different
142
- const currentName = props . name
143
- const matchedRoute = matchedRouteRef . value
159
+ currentName = props . name
160
+ matchedRoute = matchedRouteRef . value
161
+
144
162
const ViewComponent =
145
163
matchedRoute && matchedRoute . components ! [ currentName ]
146
164
@@ -149,7 +167,8 @@ export const RouterViewImpl = /*#__PURE__*/ defineComponent({
149
167
}
150
168
151
169
// props from route configuration
152
- const routePropsOption = matchedRoute . props [ currentName ]
170
+ // matchedRoute exists since we check with if (ViewComponent)
171
+ const routePropsOption = matchedRoute ! . props [ currentName ]
153
172
const routeProps = routePropsOption
154
173
? routePropsOption === true
155
174
? route . params
@@ -158,13 +177,6 @@ export const RouterViewImpl = /*#__PURE__*/ defineComponent({
158
177
: routePropsOption
159
178
: null
160
179
161
- const onVnodeUnmounted : VNodeProps [ 'onVnodeUnmounted' ] = vnode => {
162
- // remove the instance reference to prevent leak
163
- if ( vnode . component ! . isUnmounted ) {
164
- matchedRoute . instances [ currentName ] = null
165
- }
166
- }
167
-
168
180
const component = h (
169
181
ViewComponent ,
170
182
assign ( { } , routeProps , attrs , {
@@ -181,9 +193,10 @@ export const RouterViewImpl = /*#__PURE__*/ defineComponent({
181
193
// TODO: can display if it's an alias, its props
182
194
const info : RouterViewDevtoolsContext = {
183
195
depth : depth . value ,
184
- name : matchedRoute . name ,
185
- path : matchedRoute . path ,
186
- meta : matchedRoute . meta ,
196
+ // same as above: ensured with if (ViewComponent) above
197
+ name : matchedRoute ! . name ,
198
+ path : matchedRoute ! . path ,
199
+ meta : matchedRoute ! . meta ,
187
200
}
188
201
189
202
const internalInstances = isArray ( component . ref )
0 commit comments