Angular 5 - 路由更改网址但不导航

时间:2018-03-25 15:31:58

标签: angular typescript url angular2-routing

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

  • 应用程序模块 - 使用emtpy数组根配置
  • 主页模块 - 使用''主页comp的路径配置。搜索播放器
  • 玩家结果模块 - 显示搜索结果(玩家统计或错误页面)

现在在这个玩家搜索模块中我有一个玩家搜索结果comp与子组件。其中一个子comp是一个带有列表的模态。如果我点击此列表项(播放器),它应该导航'到父comp.and更新其视图。但唯一更新的是网址。 window.location.href(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 {
}

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

//browser is already on /pathName/oldID, 
//this means our Routes are correct
 
 let newLocation = `/pathName/${newID}`;
 // override default re-use strategy
 this.router
    .routeReuseStrategy
    .shouldReuseRoute = function () {
        return false;
 };
 this.router
   .navigateByUrl(newLocation)
   .then(
   (worked) => {
        //works only because we hooked 
        //the routeReuseStrategy.shouldReuseRoute above
   },
   (error) => {
    debugger;
    }
 );

我看到这个问题的次数比我想承认的要多,通常当我有一个父容器重用子组件时才会见到。

  • 父路由默认为xyz /。一切正常。
  • 父级将路线更改为xyz / 1以显示1号人物ID的详细信息
  • 父页面只更改网址。

这是因为Angular的默认路由重用策略不会重新加载页面!

上面的代码修复了此默认行为,但必须放入适当的组件中才能从头开始行为进行重新加载。

请注意,还有其他选择...假设您要保持组件重用的速度。在这种情况下,只需在已加载的组件中提供一个入口点即可重置数据,而使组件通过changeDetectorRef.DetectChanges()进行更改。

相关问题