通过navigationForward角度7传递参数

时间:2019-06-01 18:33:45

标签: angular

我正在尝试在路线上传递参数,但它以未定义形式到达。 在登录屏幕上,我通过NavController选项卡进行调用,但是在ActivatedRoute选项卡中,路由以未定义的方式到达。

export class TabsPage {
  logado = false;
  id=0;

  constructor(public navCtrl: NavController, private route: ActivatedRoute) 
  { 

  }

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

    if(this.id == undefined || this.id == null || this.id == NaN || this.id < 1){
      this.navCtrl.navigateForward('/login');
      this.logado = false;
    }else{
      this.logado = true;
      this.navCtrl.navigateForward('/home');
    }
}
}

1 个答案:

答案 0 :(得分:0)

将您的逻辑移动到queryParams订阅中,以便仅在设置ID后运行:

ngOnInit() {
  this.route.queryParams.subscribe(params => {
    this.id = params["id"];

    if (this.id == undefined || this.id == null || this.id == NaN || this.id < 1) {
      this.navCtrl.navigateForward('/login');
      this.logado = false;
    } else {
      this.logado = true;
      this.navCtrl.navigateForward('/home');
    }
  });
}