参数更改时不会更新路径

时间:2018-04-23 12:15:34

标签: angular

我在角度应用中使用不同的参数调用相同的组件,但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
        })

}

1 个答案:

答案 0 :(得分:0)

您可以通过以这种方式订阅route.queryParams来获取queryParams,

sessionType: string;
constructor(private route: ActivatedRoute){}

ngOnInit(){
    this.route.queryParams.subscribe(params => {
        this.sessionType = params['sessionType'];
    });
}