@@ -135,12 +135,30 @@ export const RouterViewImpl = /*#__PURE__*/ defineComponent({
135135 { flush : 'post' }
136136 )
137137
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+
138153 return ( ) => {
139154 const route = routeToDisplay . value
155+ lastMatchedRoute = matchedRoute
156+ lastCurrentName = currentName
140157 // we need the value at the time we render because when we unmount, we
141158 // 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+
144162 const ViewComponent =
145163 matchedRoute && matchedRoute . components ! [ currentName ]
146164
@@ -149,7 +167,8 @@ export const RouterViewImpl = /*#__PURE__*/ defineComponent({
149167 }
150168
151169 // 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 ]
153172 const routeProps = routePropsOption
154173 ? routePropsOption === true
155174 ? route . params
@@ -158,13 +177,6 @@ export const RouterViewImpl = /*#__PURE__*/ defineComponent({
158177 : routePropsOption
159178 : null
160179
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-
168180 const component = h (
169181 ViewComponent ,
170182 assign ( { } , routeProps , attrs , {
@@ -181,9 +193,10 @@ export const RouterViewImpl = /*#__PURE__*/ defineComponent({
181193 // TODO: can display if it's an alias, its props
182194 const info : RouterViewDevtoolsContext = {
183195 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 ,
187200 }
188201
189202 const internalInstances = isArray ( component . ref )
0 commit comments