Angular 2:未定义的路由参数值

时间:2018-06-02 03:52:34

标签: angular typescript

我是棱角分明的新手,如果我对术语的理解不正确,请纠正我。

使用示例应用程序,我需要在子组件中读取路由参数值。我能够打印“Params”对象但无法提取其中的数据。只需粘贴我的代码,以便我可以提供更多详细信息:

客户详细信息组件:

http://www.example.com =>  http://www.example.com/index.php?lang=zh

}

我正在访问的网址:http://localhost:4200/customers/1/details

Google Chrome控制台:

messages on console

我试图获取参数值,因为一天但没有运气。在这种情况下,请你指导我。

由于 Prashant

根据Sujeethan的指导更新了代码,但现在我收到id为null值。下面组件的完整代码。 Code

1 个答案:

答案 0 :(得分:1)

无论如何你的控制台显示正确的值,只是你的if条件错了,你应该分配值并检查条件

ngOnInit() {
    this.route.parent.params.subscribe((params: Params) => {
        console.log("Id value" + params['id']);
        let id = params["id"];
        if (id ==="1") {
            this._custServ.getCustomerDetailsById(1)
                .subscribe((customer: ICustomer) => {
                    this.customer = customer;
                });
        }
    });
}
相关问题