使用参数实现路由



我有一个相对完整的 Angular 4 Web 应用程序,路由从不使用参数:

domain.com/questions/
=> than you choose your questions in a html select

我需要像这样使用参数实现路由:

domain.com/questions/23/

直接讨论特定问题

在我的 23 个组件中实现"路由参数订阅"而不重复自己的最佳方法是什么?

在路由模块中使用 questions/:id。然后在你的组件中使用它

constructor(private router: Router) {}
public questionId:any;
ngOnInit(){
   this.router.params.subscribe(res => {
        questionId = res['id'];
    })
}

最新更新