内工作鉴于该应用模块:角2 router.navigate不嵌套js函数
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 {
}
鉴于下述成分:
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']);
}));
}
}
它导航到仪表板,但在信息中心的路线的消失网址。
所以我留下:http://localhost:3001/,我应该看到http://localhost:3001/dashboard。
如果我移动self._router.navigate([ '/仪表板']);在js函数之外,它工作正常。
UPDATE:
在做功能之外路线改变正常工作:(但我需要它在JS功能的回调
doLogin() {
var self = this;
self.goToRoute(); //MOVED TO HERE. WORKING FINE.
/*ExternalJS.authenticateByUser({username: this.username, password: this.password}, ((response:any) => {
self.goToRoute();
})
}));*/
}
更新3:
明白了工作基础上冈特评论:
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: 添加区域后,散列仍然消失。我将此添加到应用模块:
providers: [
{provide: LocationStrategy, useClass: HashLocationStrategy}
]
,并解决了问题。
不知您的路由设置? –
UPPATE 4: 添加区域后,散列仍然消失。我将此添加到应用程序模块: 提供程序:[ {provide:LocationStrategy,useClass:HashLocationStrategy} ], –