0

我想要做的是将数据从内部组件传递给路由..

在应用程序组件 ts 我有这个

this.router.events.subscribe(event => {
  if (event instanceof RoutesRecognized) {
    let current_route = event.state.root.firstChild;
    const title = current_route?.data.title;
    this.titleService.setTitle(title);
  }
});

然后像这样的路线

  { path: 'confirm-registration/:code', component: HomeComponent,canActivate:[HomeGuard],data:{title:'title.landing_page'} },
  { path: 'u/:user', component: ProfileComponent},

在配置文件组件中,我需要以某种方式将标题传递给路由..

我尝试将其放入 ngOnInit 中,但它不起作用

this.router.navigate([decodeURI(this.router.url)],{state: {title: "Test Title"}});

所以我想问是否有可能以某种方式实现这一目标。

编辑

我试过以下

let currentRoutes = this.router.config;
currentRoutes[1].data = {title: res.data.name};
this.router.resetConfig(currentRoutes);
this.router.navigate([this.router.url]);

它成功更改了数据,但有问题,所以它只是保持无限导航

4

1 回答 1

0

那不应该在 ngOnInit() 中,它是一个生命周期钩子。

如果您在按钮单击上进行路由,那么最好编写一个在单击事件上运行的函数,并且在该函数内部,应该粘贴此行。

于 2021-08-12T14:28:22.533 回答