Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度路由器不';t首次从一个组件导航到另一个组件_Angular_Typescript - Fatal编程技术网

Angular 角度路由器不';t首次从一个组件导航到另一个组件

Angular 角度路由器不';t首次从一个组件导航到另一个组件,angular,typescript,Angular,Typescript,我想在注册后从注册组件重定向到挑战组件。如果本地存储未设置到质询组件中,它将重定向到注册组件。我的问题是它不会在第一次注册时从注册导航到挑战组件 challange.component.ts ngOnInit() { if(localStorage.getItem('userId') != null) { ...... } else{ this.router.navigate(['/signup']); } } signupNew()

我想在注册后从注册组件重定向到挑战组件。如果本地存储未设置到质询组件中,它将重定向到注册组件。我的问题是它不会在第一次注册时从注册导航到挑战组件

challange.component.ts

ngOnInit() {
    if(localStorage.getItem('userId') != null) {
     ......
    }
    else{
      this.router.navigate(['/signup']);
    }

  }
signupNew() {
    this.signupService.doSignup(this.parameter);
    this.router.navigate(['/challange']);
}
signup.component.ts

ngOnInit() {
    if(localStorage.getItem('userId') != null) {
     ......
    }
    else{
      this.router.navigate(['/signup']);
    }

  }
signupNew() {
    this.signupService.doSignup(this.parameter);
    this.router.navigate(['/challange']);
}
注册服务

doSignup(signParam : SignupParam) {
    this.signupCollection.add(signParam).then(ref => {
      localStorage.setItem('userId', ref.id);
      console.log(localStorage.getItem('userId'))
    }).catch(err => {
      console.log('Check your Internet connection...');
    })
  }
路线1.ts

export const appRoutes : Routes = [
    { path : 'signup', component : SignupComponent },
    { path : 'challange', component: ChallangeComponent},
    { path : 'feed', component: NewsFeedComponent},
    { path : 'items-initial', component: InitialItemComponent},
    { path: '', redirectTo: '/', pathMatch : 'full'}
];

我认为你的问题在于:

this.signupService.doSignup(this.parameter);
this.router.navigate(['/challange']);
doSignup
是异步的,您没有同步它,因此在本地存储中设置
userId
之前,路由器正在导航到
/challange
,这会导致第一次重新定向。但是,此时,
userId
将位于本地存储中。同步这些呼叫:

async doSignup(signParam : SignupParam) {
  // error handling omitted
  const ref = await this.signupCollection.add(signParam);
  localStorage.setItem('userId', ref.id);
}

async signupNew() {
  await this.signupService.doSignup(this.parameter);
  this.router.navigate(['/challange']);
}

你能给我们看看你的机械配置吗?你有什么错误吗?是否调试代码以查看正在调用router.navigate代码?当我单击“挑战”菜单时,它将直接重定向到注册。注册后,它将不会重定向。然后我再次注册,它将重定向。所以我要面对两个报名者。没有发生错误我可以从服务文件重定向更多内容吗?@Rijo您可以从服务重定向,因为
路由器可以注入服务,但我个人不会在服务中做与UI相关的事情,比如路由,因为这样路由功能与注册功能紧密耦合,如果您想添加没有此特定路由的用户,那么您将失去灵活性。只是要记住一点。我有麻烦它不工作。我重新编写的服务文件“async DosGnup(signParam:SignupParam){等待这个.signupCollection.add(signParam).then(ref=>{localStorage.setItem('userId',ref.id');console.log(localStorage.getItem('userId'))}.catch(err=>{console.log('Check your your internetconnection…);})”signup.component.ts async signupNew(){if(this.parameter.firstName!=''){等待this.signupService.doSignup(this.parameter);this.parameter.firstName='';this.router.navigate(['/challange'];}}}它没有导航您确定它正在到达
this.router.navigate
行吗?顺便说一句,它说的是挑战而不是挑战,所以不确定这是否是一个打字错误。我刚从你的问题中抄写出来