简体   繁体   English

如何从导航触发的路由解析器访问先前激活的组件?

[英]How to access previous activated component from a route resolver triggered by a navigation?

Here is a container component which has different child routes ( router-outlet ) can navigate within its child component (assume component not change when loading, only change the input param after navigate).这是一个容器组件,它具有不同的子路由( router-outlet )可以在其子组件中导航(假设加载时组件不改变,只在导航后改变输入参数)。

Now I need to access the current child component properties when the router resolving data.现在我需要在路由器解析数据时访问当前的子组件属性。 But look at the blow console.log(), the snapshot component only shows the constructor attributes, instead of the full component properties, methods defined inside of it.但是看看控制台.log(),快照组件只显示构造函数属性,而不是完整的组件属性,在其中定义的方法。

@Injectable()
export class ReportResolverService implements Resolve<any> {

  constructor(
    private router: Router, public route: ActivatedRoute
  ) { }

  resolve() {
    const snapshot = this.route.snapshot;
    console.log(snapshot.component);
  }
}

The screenshot is as follow:截图如下:

在此处输入图片说明

It shows only the component constructor properties, not the whole class properties.它只显示组件构造函数的属性,而不是整个类的属性。

Even I change the injection of route as a param of resolve()即使我将route的注入更改为resolve()的参数

The component property on ActivatedRouteSnapshot is of below type:- ActivatedRouteSnapshot 上的组件属性属于以下类型:-

component: Type<any> | string | null;

and if you look at the Type interface description below:-如果您查看下面的Type接口描述:-

( https://angular.io/api/core/Type#description ), https://angular.io/api/core/Type#description ),

it will represent the constructor function.它将代表构造函数。 I believe that's why it shows constructor instead of class properties.我相信这就是为什么它显示构造函数而不是类属性的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM