我想在Angular引导的组件中读取可选的URL参数,无论是查询还是矩阵参数,这是/在ngOnInit()中 .
据我所知,“ActivatedRoute”在未在插座中加载的组件中不可用,因此使用index.html如:
<body>
<app-root>Loading...</app-root>
</body>
和app.module.ts文件包含:
bootstrap: [AppComponent]
是否以及如何从http://localhost:4200/my-component/id;param1=abc访问参数?
@Component({
selector: 'app-root',
template: '<router-outlet></router-outlet>',
styles: []
})
export class AppComponent implements OnInit {
constructor(private router: Router) {
... ?
}
ngOnInit(): void {
... ?
}
}
2 回答
关于Angular 2 - How to pass URL parameters?,提供的解决方案不适用于我的原始问题,但是Angular Route guards是正确的功能 .
通过路径保护,您可以在组件处理前拦截和捕获路径信息 .