I call the same component in my angular app with different parameters but the ngOnInit
is called only once. How can I solve this? The purpose is that different data is shown depending the parameter
const routes: Routes = [{
path: 'overviewscorings',
component: MobileSessionOverviewComponent,
pathMatch: 'full'
];
Template :
<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>
Component :
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
})
}
You can get queryParams by subscribing route.queryParams in this way,
sessionType: string;
constructor(private route: ActivatedRoute){}
ngOnInit(){
this.route.queryParams.subscribe(params => {
this.sessionType = params['sessionType'];
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.