Search code examples
angular7-router

data in router is undefined in angular 7


I have a route like below:

{ path: 'contact-us', component: ContactUsComponent , data: {isPrivate: false}},

When I try to get the above isPrivate value on ngOnInit() of ContactUsComponent.ts, it gives undefined:

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

Solution

  • Try getting the property right in the constructor:

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