我在角度应用中使用不同的参数调用相同的组件,但ngOnInit
只调用一次。
我怎么解决这个问题?
目的是根据参数
const routes: Routes = [{
path: 'overviewscorings',
component: MobileSessionOverviewComponent,
pathMatch: 'full'
];
模板:
<ul class = "dropdown-menu">
<li> <a[routerLink] = "['/overviewscorings']"[queryParams] = "{sessionType : 'CHAR'}"> Characteristics </a> </li>
<li> <a[routerLink] = "['/overviewscorings']"[queryParams] = "{sessionType : 'BOL'}"> Bolters </a> </li>
<li> <a[routerLink] = "['/overviewscorings']"[queryParams] = "{sessionType : 'GAP'}"> Gap </a> </li>
</ul>
组件:
export class MobileSessionOverviewComponent implements OnInit {
fieldMobileSessions: FieldMobileSession[] = [];
selectedItems:
FieldMobileSession[] = [];
sessionType: string;
constructor(private route: ActivatedRoute) {
this.route
.queryParams
.map(params = params['sessionType'])
.subscribe(stats = this.sessionType = stats);
console.log('type ' + this.sessionType); // let fieldMobileSessions = fieldTrialDb.fieldMobileSessions;
this.find();
}
ngOnInit(): void {
console.log('init');
//this.load().subscribe(s =this.fieldMobileSessions = s);
this.route.params.subscribe((params: Params) = {
this.sessionType
})
}
答案 0 :(得分:0)
您可以通过以这种方式订阅route.queryParams来获取queryParams,
sessionType: string;
constructor(private route: ActivatedRoute){}
ngOnInit(){
this.route.queryParams.subscribe(params => {
this.sessionType = params['sessionType'];
});
}