Skip to content

Commit 3b16bf1

Browse files
LinusBorgyyx990803
authored andcommitted
docs: improve in-component guard examples (#1704)
* Improve in-component guard examples * Update navigation-guards.md
1 parent 3cbc0f3 commit 3b16bf1

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

Diff for: docs/en/advanced/navigation-guards.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,28 @@ beforeRouteEnter (to, from, next) {
116116
}
117117
```
118118

119-
You can directly access `this` inside `beforeRouteLeave`. The leave guard is usually used to prevent the user from accidentally leaving the route with unsaved edits. The navigation can be canceled by calling `next(false)`.
119+
Note that `beforeRouteEnter` is the only hook that supports passing a callback to `next`. For `beforeRouteUpdate` and `beforeRouteLeave`, `this` is already available, so passing a callback is unnecessary and therefore *not supported*:
120+
121+
```js
122+
beforeRouteUpdate (to, from, next) {
123+
// just use `this`
124+
this.name = to.params.name
125+
next()
126+
}
127+
```
128+
129+
The **leave guard** is usually used to prevent the user from accidentally leaving the route with unsaved edits. The navigation can be canceled by calling `next(false)`.
130+
131+
```js
132+
beforeRouteLeave (to, from , next) {
133+
const answer = window.confirm('Do you really want to leave? you have unsaved changes!')
134+
if (answer) {
135+
next()
136+
} else {
137+
next(false)
138+
}
139+
}
140+
```
120141

121142
### The Full Navigation Resolution Flow
122143

0 commit comments

Comments
 (0)