angular 5导航菜单路径查询

时间:2018-05-28 13:15:01

标签: angular

我正在尝试在导航菜单中获取Angular路径查询。每当我尝试

this.route.paramMap.subscribe(params =>
            this.Name = params.get('name')
        );

我得到未定义或null。

完整代码

constructor(private route: ActivatedRoute) {}

ngOnInit() {
    var name;

   this.route.paramMap.subscribe(params =>
        name = params.get('name')
    );

    alert(name);
}

3 个答案:

答案 0 :(得分:0)

this.route.paramMap
      .switchMap((params: ParamMap) => {
          return Observable.of(params);
      }).subscribe(() => {
          name = params.get('name')
});

试试这个!

答案 1 :(得分:0)

你可以尝试

name=""
this.route.paramMap.subscribe(param => {
    this.name = param.get('name');
});

答案 2 :(得分:0)

 public constructor(private route:ActivatedRoute, private router:Router) {
      console.log(route.snapshot.data['name']);
  }