Angular router doesn't navigate for the first time from component to component

Rijo

I want to redirect from signup component to challenge component after signup. If the local storage not set into the challenge component it will redirect to the signup component. My problem is to it will not navigate from signup to challenge component for the first time signup.

challange.component.ts

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

  }

signup.component.ts

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

signup.service.ts

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...');
    })
  }

route.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'}
];
Explosion Pills

I think your issue is here:

this.signupService.doSignup(this.parameter);
this.router.navigate(['/challange']);

doSignup is asynchronous and you are not synchronizing it, so the router is navigating to /challange before userId is set in local storage which leads to another redirect the first time. However, at that point, the userId would be in localStorage. Synchronize these calls:

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']);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular2 - named router-outlet doesn't navigate to component with child route

Angular router doesn't see component to redirectTO

Angular 7 navigate is changing URL accordingly BUT doesn't load the component

Trying to navigate from a component that doesn't have the navigation props

React Router doesn't load the new page component after redirecting via navigate

Angular 7 Navigate from component to another

Component dynamic import doesn't work from router.js

React router doesn't load page from other component page

Angular 6 same router.navigate causes refresh when called from another component

how to navigate from sibling component to another sibling component in Angular?

Save data from an Angular 2 component before navigate to another component

How to navigate from one component to a particular division of another component in angular?

Angular Router navigate and reload need to render the component once but renders twice

Router navigate not hitting component method

Angular 4 navigate method doesn't render page contents on the target component (ngIf, ngFor)

VueJS Router doesn't load the component

Router View in a child of a component doesn't work

react router component props doesn't accept <Link /> component?

Component constructor and Router life-cycle hooks not triggered when calling router.parent.navigate method from child component in Angular2 routing

Angular2 router.navigate() not working first time

Angular: component doesn't retrieve data from service

Angular 2: access an element from the Component, getDocumentById doesn't work

Observer subscribe doesn't load first time, not firing on first load to Component

Vuejs - Router doesn't render third component in app. First two are working

Can't install Angular 1.5 component router

Blazor Component value doesn't update first time but updates second time

I am working on an Angular project. I need to pass an array from one component to another using router.navigate but unable to do it

angular router how to call child component from the parent component

Nativescript angular navigate to previous component