0
を働いていない私は、私のルータを行うにはangular2 RC3とangular2 /ルータ3.0.0-alpha.8を使用します。 ここでは私のコードです:angular2ルートが
bootstrap(AppComponent, [HTTP_PROVIDERS, APP_ROUTER_PROVIDERS, AUTH_PROVIDERS,
{
provide: AuthHttp,
useFactory: (http:Http) => new AuthHttp(new AuthConfig({tokenName: 'jwt'}), http),
deps: [Http]
},
{
provide: TranslateLoader,
useFactory: (http:Http) => new TranslateStaticLoader(http, 'app/i18n', '.json'),
deps: [Http]
},
{provide: LocationStrategy, useClass: PathLocationStrategy},
// use TranslateService here, and not TRANSLATE_PROVIDERS (which will define a default TranslateStaticLoader)
TranslateService,
ApiService,
CookieService,
AuthenticationService
]).catch(err => console.error(err));
私のルータは次のとおりです。
export const routes:RouterConfig = [
...AccountRouters,
...DealOverviewRouters
];
export const APP_ROUTER_PROVIDERS = [
provideRouter(routes)
];
export const AccountRouters: RouterConfig = [
{
path: 'account', component: AccountComponent
},
{
path: 'login', component: LoginComponent
},
];
export const DealOverviewRouters:RouterConfig = [
{
path: '',
redirectTo: '/deal-overview',
terminal: true
},
{
path: 'deal-overview', component: DealOverviewComponent
}
];
その後、私のapp.component.tsで
:
constructor(public translate:TranslateService, private _service: AuthenticationService, private _router: Router) {
this.isLogin = _service.isLogin;
console.log(_service.isLogin);
}
ngOnInit() {
// this._service.checkCredentials();
if(!this.isLogin) {
console.log(1111);
this._router.navigateByUrl('/login');
}
}
それは本当に私のコンソールログに1111
を印刷しています。 ですが、/login
へのリダイレクトは機能しません。
localhost:3000/login
ページに直接アクセスできますが、何もエラーはありません。 ですが、this._router.navigateByUrl('/login')
は機能しません。 1つのconstにそれらすべてを配置して、それをしようと手動で、代わりに
export const routes:RouterConfig = [
...AccountRouters,
...DealOverviewRouters
];
:
あなたは '持っていますか'index.html'の'
'タグの