Angular4 中的 ActivatedRoute 和 ActivatedRouteSnapshot 有什么区别 [英] What is the difference between ActivatedRoute and ActivatedRouteSnapshot in Angular4
问题描述
Angular 4 中的 ActivatedRouteSnapshot
和 ActivatedRoute
有什么区别?据我了解,ActivatedRouteSnapshot
是 ActivatedRoute
的子代,这意味着 ActivatedRoute
包含 ActivatedRouteSnapshot
.
What is the difference between ActivatedRouteSnapshot
and ActivatedRoute
in Angular 4? It's my understanding that ActivatedRouteSnapshot
is a child of ActivatedRoute
, meaning that ActivatedRoute
contains ActivatedRouteSnapshot
.
顺便说一句,我尝试运行 Google 搜索来寻找这个问题的答案,但我没有发现任何搜索结果是可以理解的.
Incidentally, I tried running a Google search for an answer to this question, but I didn't find any of the search results to be understandable.
谢谢!
推荐答案
自 ActivatedRoute
可以重复使用,ActivatedRouteSnapshot
是一个不可变对象,代表ActivatedRoute的特定版本代码>.它将所有与
ActivatedRoute
相同的属性公开为普通值,而 ActivatedRoute
将它们公开为可观察的.
Since ActivatedRoute
can be reused, ActivatedRouteSnapshot
is an immutable object representing a particular version of ActivatedRoute
. It exposes all the same properties as ActivatedRoute
as plain values, while ActivatedRoute
exposes them as observables.
这是实现中的注释:
export class ActivatedRoute {
/** The current snapshot of this route */
snapshot: ActivatedRouteSnapshot;
如果路由器重用了一个组件并且没有创建新的激活路由,那么对于同一个ActivatedRoute
,您将有两个版本的ActivatedRouteSnapshot
.假设您有以下路由配置:
If a router reuses a component and doesn't create a new activated route, you will have two versions of ActivatedRouteSnapshot
for the same ActivatedRoute
. Suppose you have the following routing configuration:
path: /segment1/:id,
component: AComponent
现在您导航到:
/segment1/1
您将在 activatedRoute.snapshot.params.id
中将参数设为 1
.
You will have the param in the activatedRoute.snapshot.params.id
as 1
.
现在您导航到:
/segment1/2
您将在 activatedRoute.snapshot.params.id
中将参数设为 2
.
You will have the param in the activatedRoute.snapshot.params.id
as 2
.
您可以通过执行以下操作来查看它:
You can see it by implementing the following:
export class AComponent {
constructor(r: ActivatedRoute) {
r.url.subscribe((u) => {
console.log(r.snapshot.params.id);
});
这篇关于Angular4 中的 ActivatedRoute 和 ActivatedRouteSnapshot 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!