angular 7 中未定义路由器中的数据

data in router is undefined in angular 7

我有如下路线:

{ 路径:'contact-us',组件:ContactUsComponent,数据:{isPrivate: false}},

当我尝试在 ContactUsComponent.ts 的 ngOnInit() 上获取上述 isPrivate 值时,它给出未定义的:

constructor(private route: ActivatedRoute) {}
..
ngOnInit() {
 this._private = this.route.snapshot.data['isPrivate'];
}

看看这个

constructor(router:Router, route: ActivatedRoute) {
    router.events
      .filter(e => e instanceof NavigationEnd)
      .forEach(e => {
        this.title = route.root.firstChild.snapshot.data['PageName'];
    });
}

尝试在构造函数中正确获取 属性:

constructor(private route: ActivatedRoute) {
    this._private = this.route.snapshot.data['isPrivate'];
}

也许可以试试:-

constructor(private route: ActivatedRoute) {}
OnInit() {
  this.route.data.subscribe(private => this._private = private);
}