Skip to content

Commit 6400619

Browse files
committed
fix(guards): deactivate leave and update guards
1 parent 8860728 commit 6400619

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

e2e/guards-instances/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ leaves: {{ state.leave }}
141141
<li><router-link to="/f/2">/f/2</router-link></li>
142142
<li><router-link to="/f/2?bar=foo">/f/2?bar=foo</router-link></li>
143143
<li><router-link to="/f/2?foo=key">/f/2?foo=key</router-link></li>
144+
<li><router-link to="/f/2?foo=key2">/f/2?foo=key2</router-link></li>
144145
</ul>
145146
146147
<template v-if="testCase === 'keepalive'">

e2e/specs/guards-instances.js

+30
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ module.exports = {
130130
.assert.containsText('.view', 'foo 1')
131131
.click('li:nth-child(2) a')
132132
.assert.containsText('.view', 'foo 1')
133+
.click('li:nth-child(6) a')
134+
.click('#resetLogs')
135+
.click('li:nth-child(7) a')
136+
.assert.containsText('.view', 'foo 0')
137+
.expect.element('#logs')
138+
.text.to.equal(
139+
['update /f/2 - /f/2', 'setup:update /f/2 - /f/2'].join('\n')
140+
)
141+
browser.click('li:nth-child(6) a').assert.containsText('.view', 'foo 0')
133142

134143
browser.end()
135144
},
@@ -168,6 +177,27 @@ module.exports = {
168177
.click('li:nth-child(2) a')
169178
.click('li:nth-child(6) a')
170179
.assert.containsText('.view', 'foo 2')
180+
.click('#resetLogs')
181+
.click('li:nth-child(7) a')
182+
.assert.containsText('.view', 'foo 0')
183+
.expect.element('#logs')
184+
// should only trigger active guards
185+
.text.to.equal(
186+
['update /f/2 - /f/2', 'setup:update /f/2 - /f/2'].join('\n')
187+
)
188+
browser
189+
.click('li:nth-child(6) a')
190+
.assert.containsText('.view', 'foo 2')
191+
.expect.element('#logs')
192+
.text.to.equal(
193+
[
194+
'update /f/2 - /f/2',
195+
'setup:update /f/2 - /f/2',
196+
// we won't see the update guard because the instance is not available
197+
// 'update /f/2 - /f/2',
198+
'setup:update /f/2 - /f/2',
199+
].join('\n')
200+
)
171201

172202
browser.end()
173203
},

src/navigationGuards.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,24 @@ import {
1717
NavigationFailure,
1818
NavigationRedirectError,
1919
} from './errors'
20-
import { ComponentOptions, onUnmounted } from 'vue'
20+
import { ComponentOptions, onUnmounted, onActivated, onDeactivated } from 'vue'
2121
import { inject, getCurrentInstance, warn } from 'vue'
2222
import { matchedRouteKey } from './injectionSymbols'
2323
import { RouteRecordNormalized } from './matcher/types'
2424
import { isESModule } from './utils'
2525

2626
function registerGuard(list: NavigationGuard[], guard: NavigationGuard) {
27-
onUnmounted(() => {
27+
const removeFromList = () => {
2828
const index = list.indexOf(guard)
2929
if (index > -1) list.splice(index, 1)
30+
}
31+
32+
onUnmounted(removeFromList)
33+
onDeactivated(removeFromList)
34+
35+
onActivated(() => {
36+
const index = list.indexOf(guard)
37+
if (index < 0) list.push(guard)
3038
})
3139

3240
list.push(guard)

0 commit comments

Comments
 (0)