角4中ActivatedRouteSnapshotActivatedRoute的区别是什么?我的理解是ActivatedRouteSnapshotActivatedRoute的子元素,也就是说ActivatedRoute包含ActivatedRouteSnapshot.

顺便说一句,我试着在谷歌上搜索这个问题的答案,但我没有发现任何搜索结果是可以理解的.

非常感谢.

推荐答案

因为ActivatedRoutecan be reusedActivatedRouteSnapshot是表示ActivatedRoute中的a particular version的不可变对象.它将所有与ActivatedRoute相同的属性公开为普通值,而ActivatedRoute将它们公开为可观察的.

以下是实施中的 comments :

export class ActivatedRoute {
  /** The current snapshot of this route */
  snapshot: ActivatedRouteSnapshot;

如果路由重用了一个组件,并且没有创建新的激活路由,那么对于相同的ActivatedRoute,您将拥有两个版本的ActivatedRouteSnapshot.假设您具有以下路由配置:

path: /segment1/:id,
component: AComponent

现在导航到:

/segment1/1

你将在activatedRoute.snapshot.params.id as 1中拥有参数.

现在导航到:

/segment1/2

您将得到activatedRoute.snapshot.params.id号中的参数为2号.

您可以通过实现以下内容来查看它:

export class AComponent {
  constructor(r: ActivatedRoute) {    
    r.url.subscribe((u) => {
      console.log(r.snapshot.params.id);
    });

Angular相关问答推荐

*ngFor循环中令人困惑的Angular 行为

try 测试具有Angular material 下拉列表的组件时,在Angular 17中发现&q;错误的意外合成属性@TransitionMessages

声明Angular material 中按钮的自定义 colored颜色

Component RegisterComponent模板出现Angular 错误

P-DropDown不会使用react 式表单手动设置Value

如何使用来自不同可观测对象的值更新可观测对象

Angular 懒惰地加载一个服务

Angular-每2个流式传输多个HTTP调用

区分material 手风琴

NgRx Effects 抛出类型错误,我不知道发生了什么

Angular 迁移 14 到 15 / 16:angular universal 是否停止将 <!-- this page was prerendered with angular universal --> 用于预渲染页面?

完成行为主体

为什么 HttpParams 在 Angular 4.3 中的多行中不起作用

无法获得 angular-material md-sidenav 的 100% 高度

Angular 没有可用于依赖类型的模块工厂:ContextElementDependency

来自 ngFor 项的动态 routerLink 值给出错误Got interpolation ({{}}) where expression was expected

Angular 应用程序中的语法错误:Unexpected token <

在 Angular 2 中使用 store (ngrx) 有什么好处

如何在 Angular 2 中链接 HTTP 调用?

如何从指令访问主机组件?