2017-10-20 101 views
0

我有以下路由器:角4路由器:入門重定向孩子的第一路中

const routes: Routes = { 
    { 
     path: '', 
     pathMatch: 'full', 
     redirectTo: 'en' 
    }, 
    { 
     path: 'en', 
     children: [ 
      { 
        path: '', 
        pathMatch: 'full', 
        redirectTo: 'homepage' 
      }, 
      { 
        path: 'homepage', 
        component: HomeComponent 
      }, 

     ] 
    }, 

} 

我想會是什麼,當我去我http://example.com我被重定向到/en/homepage直接。 現在,發生了什麼是我只被重定向到/en。我在路由器配置中缺少什麼?

+0

redirectTo: '/ EN'和redirectTo:'/ en/homepage'。默認情況下,重定向不是相對的,好像你期待的那樣 –

回答

1

嘗試這樣的:

更新了這個答案嘗試像這樣在你的路線文件,如下面將正常工作對我來說模塊文件。

home.route.ts

export const routes: Routes = [ 
    { 
     path: '', 
     pathMatch: 'full', 
     redirectTo: 'en' 
    }, 
    { 
     path: 'en', 
     children: [ 
      { 
       path: '', 
       pathMatch: 'full', 
       redirectTo: 'homepage' 
      }, 
      { 
       path: 'homepage', 
       component: HomeComponent 
      } 

     ] 
    } 
] 

module.ts

import { NgModule } from '@angular/core'; 
import { routes } from './home.route'; 
const HOME_STATES = [ 
    ...routes 
]; 

@NgModule({ 
    imports: [ 
     RouterModule.forRoot(HOME_STATES, { useHash: true }) 
    ] 
}) 

export class HomeModule { } 
+0

這很不幸沒有工作 – Scipion

+0

如果你去輸入這個url http://example.com它會自動重新加載到http://example.com/en只要 ? – Chandru

+0

是的,雖然我希望它去/ en /主頁 – Scipion

0

的解決方案是重定向到所需的路徑:

{ 
     path: '', 
     pathMatch: 'full', 
     redirectTo: '/en/homepage' 
} 
+0

如果我在appComponent中的運行時添加/ homepage路線怎麼辦? – Scipion

+0

路由將重定向到您在redirectTo屬性中提到的路徑,而不進行任何驗證。 –