Angular 5 Router navigate from callback do nothing

satis

I'm stuck on navigation from callback function, router.navigate do nothing.

I saw in stackoverflow that using NgZone may help but still no success, only thing which working is using window.location.href = 'url...'

but this is not angular functions and i really want to understand what wrong.

Here is my code.

@NgModule({
  declarations: [
    AppComponent,
    LoginComponent,
    ChatComponent,
    NotFoundComponent,
    HomeComponent
  ],
  imports: [
    BrowserModule,
    RouterModule.forRoot([
      { path: '', component: HomeComponent },
      { path: 'login', component: LoginComponent },
      {path:'chat',component:ChatComponent},
      { path: '**', component: NotFoundComponent }
    ])
  ],
  providers: [],
  bootstrap: [AppComponent]
})

component:

import { ActivatedRoute, Router } from '@angular/router';

constructor(private router: Router,
    private route: ActivatedRoute,
    private zone: NgZone) { }

Parse.FacebookUtils.logIn('email,user_birthday,user_friends', {
      success: function (user) {
        // Handle successful login
        console.log(user);
        this.router.navigate(['/']);

      },error: function (user, error) {
        // Handle errors and cancellation
      }
    });

Also tried like this ans still no success:

this.zone.runTask(()=>{
      this.router.navigate(['/']);
    })
dlcardozo

It seems that you are loosing your context, try with a fat arrow in the callback.

Parse.FacebookUtils.logIn('email,user_birthday,user_friends', {
  success: (user) => {
    // Handle successful login
    console.log(user);
    this.router.navigate(['/']);

  },error: (user, error) => {
    // Handle errors and cancellation
  }
});

When you use a callback you must be sure that it will use your context, the current this should be your component, in typescript you will use the fat arrow operator (=>) that has the same effect of the vanilla js function.bind(this) to be sure that the code in the callback will use the right this, otherwise the thisobject of the callback will be the window object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular 5 router.navigate does not work

router.navigate with query params Angular 5

this.router.navigate does nothing

Angular4 router breaks after using `router.navigate()` in Google Sign In callback

angular router navigate then reload

Angular 5 commands.reduce is not a function in router.navigate

How to navigate the component from NVD3 Callback in Angular 4?

Navigate to parent path from router outlet (NG5)

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

Using the angular-ui-router to navigate from ionic footers

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 2 router.navigate doesn't work with electron IPC callback

angular2 router.navigate inside auth0 callback

Navigate from within Promise callback

Angular router.navigate() delta

Angular 2 router.navigate

Angular2 router and navigate

karma angular test router navigate

Angular 5: Getting 'SyntaxError: Unexpected token )' when doing router.navigate()

Angular 4/5 passing multiple, non-sequential params using router.navigate?

Why is Angular router.navigate() not navigating?

angular routing using router.navigate

Angular Change Detection not triggering on Router.navigate()

Navigate relative with Angular 2 Router (Version 3)

Router navigate to child route - Angular 6

Router not resolving navigate Angular with AngularFirestore returns

Angular - Router navigate canceled without reason

Conditionally navigate to state with angular ui-router

Angular navigation issue (router.navigate)