2016-08-30 14 views
0

Iユーザーがゲストの場合は、次の式を持っている:私のapp.component.tsで角度2 RC5のrouter.navigate動作しない

this.router.navigate(['/intro']); 

ngOnInit()方法。

私app.component.htmlは<router-outlet></router-outlet>

を持っていますが、私はゲストとして私のアプリにアクセスした場合、IntroComponentが表示されず、URLは/introない空のままです。

どういうところが間違っていますか?

私app.routes.ts

const appRoutes: Routes = [ 
    { path: '', component: HomeComponent }, 
{path:'intro',component: IntroComponent}, 
{path:'login',component: LoginComponent}, 
{path:'register', component: RegisterComponent} 
]; 
+0

どのバージョンのAngularを使用していますか?ルートが設定されていますか?あなたはあなたのバージョンを適切にインポートしていますか? –

+0

ルートが設定されています、RC.5。 app.routes.tsを追加しましたHomeCOmponentが読み込まれましたthis.routerでイントロにナビゲートしていますが – TheUnreal

+0

最小の例をプランナーに貼り付けてください。 – mxii

答えて

0

ngOnInitからthis.router.navigate(['/intro'])を呼び出すときに、最初のナビゲーションが完了していないためです。

これを解決するには、2つの方法があります。router.navigate failing silently on OnInit of component

溶液#2app.module.ts

溶液#1

は、あなたが最初のナビゲーション

RouterModule.forRoot(appRoutes, { initialNavigation: false }) 

文献を無効にします

初期ナビゲーションを無効にしない場合は、setTimeoutを使用して最初のナビゲーションを完了させます。

setTimeout(() => this.router.navigate(['/intro']), 1) 
1
は、このようなあなたのルートを変更し

const appRoutes: Routes = [ 
{ path: '', redirectTo: HomeComponent, pathMatch: 'full'}, //(redirectTo or component) 
{path:'intro',component: IntroComponent}, 
{path:'login',component: LoginComponent}, 
{path:'register', component: RegisterComponent} 
]; 

デフォルトpathMatch戦略はルートでprefixあるのでpath: ''prefixと実質的にeveryrouteを意味します。だから、全ルートはHomeComponentにナビゲートされました。

関連する問題