【问题标题】:Angular cannot get the @inputData for routing - getCurrentNavigation() method returns null valueAngular 无法获取路由的 @inputData - getCurrentNavigation() 方法返回空值
【发布时间】:2020-02-14 00:00:51
【问题描述】:
<app-example [id]="requestId"></app-example>

我们已经在另一个组件中添加了选择器并传递了 id。

export class exampleComponent implements OnInit {
    @Input() id:number;

Id:string;

}
constructor(private fb: FormBuilder, private _AppService: AppService, private router: Router, private route: ActivatedRoute) {
        this.router.getCurrentNavigation().extras.state != undefined ? this.Id = this.router.getCurrentNavigation().extras.state.key : '';
        this.route.data.subscribe(data => {
            this.current_path = data.urlname;
        })
}

我们使用了 getCurrentNavigation() 方法, 但它返回空值

【问题讨论】:

标签: javascript angular typescript


【解决方案1】:

试试这样的

constructor(private router: Router) {

router.events.subscribe((event) => {
  if (event instanceof NavigationEnd) {
    this.current_path = event.url;
 }
})

【讨论】:

    猜你喜欢
    • 2019-07-20
    • 2019-03-07
    • 2016-05-06
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 2021-10-08
    相关资源
    最近更新 更多