Angular4中ActivatedRoute和ActivatedRouteSnapshot有什么区别 [英] What is the difference between ActivatedRoute and ActivatedRouteSnapshot in Angular4

查看:945
本文介绍了Angular4中ActivatedRoute和ActivatedRouteSnapshot有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Angular 4中的ActivatedRouteSnapshotActivatedRoute有什么区别?据我了解,ActivatedRouteSnapshotActivatedRoute的子级,这意味着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

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.

现在,您导航至:

/segment1/2

您将在activatedRoute.snapshot.params.id中将参数作为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆