'angular 5 nav-menu path query
I am trying to get the Angular path query in the nav menu. Whenever I try
this.route.paramMap.subscribe(params =>
this.Name = params.get('name')
);
I get undefined or null.
Full code
constructor(private route: ActivatedRoute) {}
ngOnInit() {
var name;
this.route.paramMap.subscribe(params =>
name = params.get('name')
);
alert(name);
}
Solution 1:[1]
this.route.paramMap
.switchMap((params: ParamMap) => {
return Observable.of(params);
}).subscribe(() => {
name = params.get('name')
});
Try this out!
Solution 2:[2]
You can try
name=""
this.route.paramMap.subscribe(param => {
this.name = param.get('name');
});
Solution 3:[3]
public constructor(private route:ActivatedRoute, private router:Router) {
console.log(route.snapshot.data['name']);
}
Solution 4:[4]
In case someone still looking for it. In angular 10, you can use "any". Idk if this is the only way or any better solution but it works. "any" will make angular chooses the object type that suitable for your case
name:any;
ngOnInit(): void {
this.route.paramMap.subscribe((params : ParamMap) => {
this.name = params.get('name');
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Padmapriya Vishnuvardhan |
Solution 2 | Krishna Rathore |
Solution 3 | mittal bhatt |
Solution 4 | Ray Ryzer |