gpt4 book ai didi

Angular 5 - 路由更改 url 但不导航

转载 作者:行者123 更新时间:2023-12-04 16:46:04 25 4
gpt4 key购买 nike

目前我有三个不同的模块:

  • App Module -- 清空数组 Root 配置
  • Home Module -- 带有主页组件的“”路径配置。用于搜索播放器
  • 玩家结果模块——显示搜索结果(玩家统计数据或错误页面)

现在,在这个播放器搜索模块中,我有一个带有子组件的播放器搜索结果组件。其中一个子组件是一个带有列表的模式。如果我单击此列表项(播放器),它应该“导航”到父组件并更新其 View 。但唯一更新的是 url。

window.location.href(url) 有一个变通方法,可以在导航方法更新 url 后重新加载页面。但我不喜欢变通办法。

父组件看起来很像:

export class PlayerSearchResultComponent implements OnInit, AfterViewInit {

public isLoading = true;
public searchValue: string;
public playerResult: PlayerByPlayerTagType;
public hasNoResultFound = false;
public clanInfo: ClansByClantagType;

constructor(private playerSearchService: PlayerSearchService,
private activatedRoute: ActivatedRoute,
private clanSearchService: ClanSearchService) {
}

ngOnInit(): void {
this.activatedRoute.params.subscribe((params: Params) => {
this.searchValue = params['playerId'];
});
}

ngAfterViewInit() {
this.playerSearchService.getPlayerByPlayerTag(this.searchValue).subscribe(player => {
this.playerResult = player;
if (!isUndefined(this.playerResult.clan)) {
this.clanSearchService.getClanByClanTag(this.playerResult.clan.tag).subscribe((data: ClansByClantagType) => {
this.clanInfo = data;
});
}
}, () => {
this.hasNoResultFound = true;
this.isLoading = false;
},
() => this.isLoading = false);
}
}

和子组件:

export class ClanModalComponent {

@ViewChild('childModal') childModal: ModalDirective;
@Input() playerResult: PlayerByPlayerTagType;
@Input() clanInfo: ClansByClantagType;


constructor(private router: Router) {
}

open() {
this.childModal.show();
}

memberSearch(member) {
this.childModal.hide();
this.router.navigate(['search/' + member.tag]);
}

}

播放器结果模块如下所示:

const appRoutes: Routes = [
{path: 'search/:playerId', component: PlayerSearchResultComponent}
];

@NgModule({
declarations: [
...
],
imports: [
...
RouterModule.forChild(
appRoutes
)],
providers: [
...
],
exports: []
})
export class PlayerResultModule {
}

最佳答案

因此,要根据路由参数加载,您必须订阅路由参数并加载数据。 ngAfterViewInit 不会被再次处理。因为路由更改后组件不会再次加载!

ngOnInit(): void {
this.activatedRoute.params.subscribe((params: Params) => {
this.searchValue = params['playerId'];
this.playerSearchService
.getPlayerByPlayerTag(this.searchValue)
.subscribe(player => {
this.playerResult = player;
...
...
});
}
}

关于Angular 5 - 路由更改 url 但不导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49477542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com