首页 > 解决方案 > 角度如果传递2个数据的问题

问题描述

我使用硬编码登录。我有 3 种类型的用户。

  if(this.loginForm.value.email == 'admin' && this.loginForm.value.password == 'admin'){
    console.log(`found corresponding user.`);
    this.router.navigate(['/dashboard/home']);
  }


  if(this.loginForm.value.email == 'USapparel&textile' && this.loginForm.value.password == 'admin'){
    console.log(`found corresponding user.`);
  this.router.navigate(['/dashboard/home', {company_id: '39'}]);
  }

  if(this.loginForm.value.email == 'USapparel&textile0118/003' && this.loginForm.value.password == 'admin'){
    console.log(`found corresponding user.`);
  this.router.navigate(['/dashboard/home', {company_id: '39', policy_id: 'IGT/D/PHI3/0000000001/0118/003'}]);
  }

我在一个中传递数据,我没有传递任何东西,在第二个 company_id 和第三个 company_id 和 policy_id 中。

在家庭组件中

if(this.company_id){
    this.getSamad(this.company_id);
     this.company = false;
  }
if(this.policy_id){
     this.getSamad2(this.policy_id);
    this.company = false;
  }

我有 2 个函数 getSamad 和 getSamad2 。但是当我只有 company_id 时,它也使用 getSamad2 函数。意思是我只传递 company_id 所以它会使用 getSamad 函数?但它使用 getSamad2 函数。任何人都可以帮助做到这一点吗?

标签: angular

解决方案


尝试:

if(this.policy_id) {
  // policy_id available
}
else {
  // policy_id not available
}

如果您想将其作为参数发送,那么对于第一个导航方法

this.router.navigate(['/dashboard/home', {company_id: '39', policy_id : null }]);

推荐阅读