我想要做的是将数据从内部组件传递给路由..
在应用程序组件 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]);
它成功更改了数据,但有问题,所以它只是保持无限导航