简体   繁体   中英

Angular 2 router.navigate not working inside nested js function

Given the app module:

import { NgModule }       from '@angular/core';
import { BrowserModule }  from '@angular/platform-browser';
import { FormsModule }    from '@angular/forms';
import { RouterModule }   from '@angular/router';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';

import { AppComponent }        from './app.component';
import {DashboardComponent} from "./components/dashboard/dashboard.component";
import {LogoutComponent} from "./components/logout/logout.component";
import {LoginComponent} from "./components/login/login.component";
import {HttpModule} from "@angular/http";
import {PrescribeComponent} from "./components/prescribe/prescribe.component";
//import { HeroService }         from './hero.service';

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    NgbModule.forRoot(),
    RouterModule.forRoot([
      {
        path: 'dashboard',
        component: DashboardComponent
      },
      {
        path: 'logout',
        component: LogoutComponent
      },
      {
        path: 'login',
        component: LoginComponent
      },
      {
        path: 'prescribe',
        component: PrescribeComponent
      }

    ])
  ],
  declarations: [
    AppComponent,
    DashboardComponent,
    LogoutComponent,
    LoginComponent,
    PrescribeComponent
  ],
  providers: [
    //HeroService
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule {
}

Given the following component:

import {Component} from '@angular/core';
import {Router} from "@angular/router";
declare var ExternalJS: any;

@Component({
  selector: 'my-app',
  templateUrl: 'app/components/login/login.component.tpl.html',
})
export class LoginComponent {

  public redirect;
  public username: string;
  public password: string;

  constructor(public _router: Router) {

    this.username = 'someUsername';
    this.password = 'SomePassword';


  }
  doLogin() {

    var self = this;

    ExternalJS.authenticateByUser({username: this.username, password: this.password}, (response => {


     self._router.navigate(['/dashboard']);


    }));
  }

}

It navigates to dashboard, but the dashboard route disappears in the url.

So I am left with : http://localhost:3001/ Where I should see http://localhost:3001/dashboard .

If I move the self._router.navigate(['/dashboard']); outside of the js function, it works fine.

UPDATE:

Doing the route change outside of the function works fine :( But i need it in the callback of the JS function.

 doLogin() {

    var self = this;
    self.goToRoute(); //MOVED TO HERE. WORKING FINE.

    /*ExternalJS.authenticateByUser({username: this.username, password: this.password}, ((response:any) => {   

           self.goToRoute();
         })


    }));*/
  }

UPDATE 3:

Got it working based on Gunter Comments:

doLogin() {

    ExternalJs.authenticateByUser({username: this.username, password: this.password}, (response => {
      var self = this;
      ExternalJs.setUser(12398787, "user1", function () {
        ExternalJs.subscribeEvent({
          eventName: 'user.select',
          callback: (data => {
            self.zone.run(() => {
              self._router.navigate(['./dashboard']);
            });
          })
        });
      });
    }));
  }

UPDATE 4: The hash was still dissappearing event after adding zone. I added this to app module:

providers: [
    {provide: LocationStrategy, useClass: HashLocationStrategy}
  ]

, and it fixed the issue.

You probably need to ensure the code is executed within Angulars zone, otherwise change detection won't run and this causes router.navigate() not to work as expected:

constructor(public _router: Router, private zone:NgZone) {
ExternalJS.authenticateByUser({username: this.username, password: this.password}, (response => {
  this.zone.run(() =>  
    this._router.navigate(['/dashboard']);
  });
}));

If you use => there is no need for self

I was actually able to resolve by creating this:

 goToRoute(){

    this._router.navigate(['/dashboard']);

  }

Then:

ExternalJS.authenticateByUser({username: this.username, password: this.password}, (response => {
  self.goToRoute()
}));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM