当一个组件在angular2生命周期的OnInit加载被称为 - 如果你想查询从每当一个新的组件加载,你可以编写代码里面有用户自定义的配置,喜欢的事的东西:
import { Component, OnInit } from '@angular/core'
@Component({
...
})
export class MyComponent implements OnInit {
ngOnInit() {
// write the check for the config value in here
}
}
你要做的唯一的其他东西是哪个文件用户在它定义的配置和使用进口的ngOnInit方法为您定义的每个组件中。
另一种方法是订阅由angular导出的位置服务,每次位置url更改时运行检查。为避免重复订阅,您需要将此代码放在主AppComponent文件中。唯一需要做的其他事情就是拥有一个全局服务,您可以将其导入到所有组件中,这些组件包含某种可以告诉您配置值是否设置为特定值的标志。
import { Component, OnInit } from '@angular/core'
import { Location } from '@angular/common'
... METADATA ...
export class AppComponent implements OnInit {
constructor(private location: Location) {}
ngOnInit() {
this.location.subscribe((value) => {
// do check in here/set value inside global service
});
}
喔好吧,我刚刚得到你想say.gr:-检查会话我可以创建一个单独的类并调用该类中的每个组件ngOnInit检查会话是否有或没有什么。基于这一点,我可以做我想要的行动。如果我错了,请让我知道。谢谢你的哥们 – RKD