路由器7中的数据未定义

时间:2019-06-18 10:02:57

标签: angular7-router

我的路线如下:

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

当我尝试在 ContactUsComponent.ts 的ngOnInit()上获得以上 isPrivate 值时,它给出了未定义的内容:

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

3 个答案:

答案 0 :(得分:0)

检查一下

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

答案 1 :(得分:0)

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

SyntaxError: file:///C:/Users/jans/Desktop/novAMS/demo/js/designer/startupMethods.jsx: Unexpected token, expected } (11:18)
   9 | 
  10 | class Svg extends React.Component {
> 11 |     constructor() {
     |                   ^
  12 |         super();
  13 | 
  14 |         this.displayData = [];

答案 2 :(得分:0)

也许尝试以下方法:-

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