【发布时间】:2018-03-25 15:31:58
【问题描述】:
目前我有三个不同的模块:
- App 模块 -- 使用 emtpy 数组根配置
- 主页模块 -- 带有主页 comp 的 '' 路径配置。寻找球员
- 玩家结果模块 -- 显示搜索结果(玩家统计信息或错误页面)
现在在这个播放器搜索模块中,我有一个带有子组件的播放器搜索结果组件。其中一个子组件是一个带有列表的模式。如果我单击此列表项(播放器),它应该“导航”到父组件并更新其视图。但唯一更新的是 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 {
}
【问题讨论】:
-
更改路线时您期望什么?我猜 playerId 改变了?
-
更改路由时,我希望调用父组件并更新父视图。
标签: angular typescript url angular2-routing