我在试着从一个家长到另一个家长。
例如,从下面我想从一个“仪表板”链接到另一个“仪表板”。
当我从“主页”链接到仪表板时,所有的链接都能工作。它们看起来是这样的:
<a [routerLink]="['/dashboard', dashboard.Id]">但是,当我在仪表板中并链接到另一个仪表板时,URL会改变,但是仪表板保持不变。
<a [routerLink]="['/dashboard', nextdashboard.Id]">如果我用“下一个仪表板”URL刷新页面,“下一个仪表板”将加载。
我做错了什么?
const appRoutes: Routes = [
{
path: '',
component: Home
},
{
path: 'dashboard/:id',
component: Dashboard,
children: [
{
path: 'Stores', component: StoresComponent
},
{
path: 'Websites', component: WebsitesComponent
}
]
}
]编辑:在我的仪表板组件中,我订阅了params。我还打电话给一家服务公司以获得新的订单。但是,这只在最初从家乡路由时才有效,而不是从仪表板到仪表板。
当我console.log ID时,我没有看到更新。
this.route.paramMap.subscribe(params => {this.Id = params.get('id')});
this.orderjson = this.dashboardService.getOrders().filter((obj) => obj.Id=== this.Id);发布于 2019-01-29 21:15:23
我也有类似的情况,我可能想从/product/5路由到/product/42。
我在组件中使用这样的代码:
ngOnInit(): void {
// Read the product Id from the route parameter
this.sub = this.route.paramMap.subscribe(
params => {
const id = +params.get('id');
this.getProduct(id);
}
);
}此代码监视路由参数的任何更改。当参数确实更改时,params.get从路由中获取参数,然后使用它获取该路由的数据。
因为数据是绑定在模板中的,所以新的数据就会出现。
您的代码需要如下所示:
this.route.paramMap.subscribe(
params => {
this.Id = params.get('id');
this.orderjson = this.dashboardService.getOrders().filter((obj) => obj.Id=== this.Id);
}); 当id被更改时,重新执行的只执行代码是订阅中的代码。因此,当id更改时,您的this.dashboardService不会被执行。它需要驻留在subscribe中。
https://stackoverflow.com/questions/54429350
复制相似问题