-
-
Notifications
You must be signed in to change notification settings - Fork 5k
docs(zh): translate navigation-failures.md to chinese. #3416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,66 @@ | ||||||
# 导航故障 | ||||||
|
||||||
> 3.4.0中新增 | ||||||
|
||||||
::: tip 译者注 | ||||||
_导航故障_,或者叫_导航失败_,表示一次失败的导航,原文 navigation failures,本文统一采用 _导航故障_。 | ||||||
::: | ||||||
|
||||||
当使用 `router-link` 组件时,Vue Router 会自动调用 `router.push` 来触发一次导航。 虽然大多数链接的预期行为是将用户导航到一个新页面,但也有少数情况下用户将留在同一页面上: | ||||||
|
||||||
- 用户已经位于他们正在尝试导航的页面了 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
- 一个[导航守卫](./navigation-guards.md)通过调用 `next(false)` 中断了这次导航 | ||||||
- 一个[导航守卫](./navigation-guards.md)抛出了一个错误,或者调用了 `next(new Error())` | ||||||
|
||||||
当使用 `router-link` 组件时,**这些失败都不会打印出错误**。然而,如果你使用 `router.push` 或者 `router.replace` 的话,你可能就会在控制台看到一条 _"Uncaught (in promise) Error"_ 这样的错误,后面会跟着一条更具体的消息。让我们了解如何区分 _导航故障_。 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
::: tip 背景故事 | ||||||
在 v3.2.0 中,通过使用 `router.push` 的两个可选的回调函数:`onComplete` 和 `onAbort` 来暴露 _导航故障_。从版本 3.1.0 开始,`router.push` 和 `router.replace` 在没有提供 `onComplete`/`onAbort` 回调的情况下会返回一个 _Promise_。用这个 _Promise_ 的 resolve 和 reject 来代替 `onComplete` 和 `onAbort` 的调用。 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
::: | ||||||
|
||||||
|
||||||
## 检测导航故障 | ||||||
|
||||||
_导航故障_是一个 `Error` 实例,附带了一些额外的属性。要检查一个错误是否来自于路由器,可以使用 `isNavigationFailure` 函数: | ||||||
|
||||||
```js | ||||||
import VueRouter from 'vue-router' | ||||||
const { isNavigationFailure, NavigationFailureType } = VueRouter | ||||||
|
||||||
// trying to access the admin page | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 注释需要翻译一下,下同 |
||||||
router.push('/admin').catch(failure => { | ||||||
if (isNavigationFailure(failure, NavigationFailureType.redirected)) { | ||||||
// show a small notification to the user | ||||||
showToast('Login in order to access the admin panel') | ||||||
} | ||||||
}) | ||||||
``` | ||||||
|
||||||
::: tip | ||||||
如果你忽略第二个参数:`isNavigationFailure(failure)`,那么只会检查这个错误是不是一个 _导航故障_。(言外之意:不会检查它到底是哪一种导航故障) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 翻译最好还是不要自己解释太多,改变原文的话最好给原文也提一下 PR。 |
||||||
::: | ||||||
|
||||||
## `NavigationFailureType` | ||||||
|
||||||
`NavigationFailureType` 可以帮助开发者来区分不同类型的 _导航故障_。有四种不同的类型: | ||||||
|
||||||
- `redirected`:在导航守卫中调用了 `next(newLocation)` 重定向到了其他地方。 | ||||||
- `aborted`:在导航守卫中调用了 `next(false)` 中断了本次导航。 | ||||||
- `cancelled`:在当前导航还没有完成之前又有了一个新的导航。比如,在等待导航守卫的过程中又调用了 `router.push`。 | ||||||
- `duplicated`:导航被阻止,因为我们已经在目标位置了。 | ||||||
|
||||||
## _导航故障_ 的属性 | ||||||
|
||||||
所有的导航故障都会有 `to` 和 `from` 属性,用来表达这次失败的导航的当前位置和目标位置。 | ||||||
|
||||||
```js | ||||||
// trying to access the admin page | ||||||
router.push('/admin').catch(failure => { | ||||||
if (isNavigationFailure(failure, NavigationFailureType.redirected)) { | ||||||
failure.to.path // '/admin' | ||||||
failure.from.path // '/' | ||||||
} | ||||||
}) | ||||||
``` | ||||||
|
||||||
在所有情况下,`to` 和 `from` 都是规范化的路由位置。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
中文使用
*
来标注斜体吧,可以不需要加额外的空格了。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的,顺便问一下,我要怎么修改?是在我本地改完之后push吗?对pr流程还不是太熟悉[捂脸]