2016-12-15 49 views
3

我的路由配置方式如下:第一个孩子航线后Angular2路由工作不被加载

RouterModule.forRoot([ 
    { path: '', redirectTo: 'main', pathMatch: 'full' }, 
    { path: 'main', component: SummaryComponent, children: [ 
     { path: 'data/:deviceId/:incidentId', component: DeviceMetricsComponent }, 
     { path: 'incident/analysis/:incidentId', component: IncidentAnalysisComponent }, 
     ] 
    }, 
    { path: 'test', component: TestFullPageComponent, children: [ 
     { path: 'test2', component: TestFullPageChildComponent} 
     ] 
    }, 

    { path: 'logs/jobs/:jobtype', component: JobLogsComponent, pathMatch: 'full' }, 
    { path: '**', redirectTo: 'main' } 
]) 

这里是我的“主”模板:

<div class="row"> 
    <div class="col-md-12"> 
     <!-- Some links are in this table that load into the router-outlet below --> 
     <app-incident-node-table [tableTitle]="incidentNodeTableTitle" [parentSubject]="incidentNodesSubject"></app-incident-node-table> 
    </div> 
</div> 
<div class="row" style="margin-top: 500px;"> 
    <div class="col-md-12"> 
     <div id="childResult"> 
      <router-outlet></router-outlet> 
     </div> 
    </div> 
</div> 

当我第一次导航到页面并单击一个链接,它按预期加载到<router-outlet>。在那一点上我的网址变得像http://localhost:4200/main/incident/analysis/3816913766390440113

该表中的任何其他链接都会显示不同的URL,但是一旦单击它们,新内容就不会加载到<router-outlet>中。

编辑: 在IncidentNodeTable模板中的链接看起来是这样的:

<span><a pageScroll [pageScrollOffset]="60" [routerLink]="['/main/incident/analysis', row.IncidentId]">Analyze</a></span> 

回答

6

原因新的内容没有被加载时您正在使用ActivatedRoute "params"这是可观察到的那么路由器可能不会重现组件导航到相同的组件时。在你的情况下,参数正在改变,没有重新创建组件。 因此,尝试这种解决方案

export class IncidentAnalysisComponent implements OnInit, OnDestroy { 
     id: number; 
     limit: number; 
     private sub: any; 

     constructor(private route: ActivatedRoute) {} 

     ngOnInit() { 
     this.sub = this.route.params.subscribe(params => { 
      this.id = +params['id']; 
      this.limit = 5; // reinitialize your variable 
      this.callPrint();// call your function which contain you component main functionality 
     }); 
     } 
    ngOnDestroy() { 
     this.sub.unsubscribe(); 
    } 
} 

我希望这会为你工作:)

相关问题