Consider 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";
@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 {
}
Now, let's focus on a specific 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']);
}));
}
}
The issue arises when navigating to the dashboard as the route disappears in the URL.
As a result, instead of seeing http://localhost:3001/dashboard, only http://localhost:3001/ is displayed.
Moving the navigation outside of the JavaScript function solves the problem. But, it needs to be inside the callback function.
In an updated version of the code provided:
doLogin() {
var self = this;
self.goToRoute(); //MOVED TO HERE. WORKING FINE.
/*ExternalJS.authenticateByUser({username: this.username, password: this.password}, ((response:any) => {
self.goToRoute();
})
}));*/
}
Following insights from Gunter's comments, here is a refined version that works efficiently:
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']);
});
})
});
});
}));
}
Further enhancements were made in Update 4 to ensure the hash remains intact after implementing zone in the app module:
providers: [
{provide: LocationStrategy, useClass: HashLocationStrategy}
]
This resolves the disappearing hash issue.