2016-11-18 71 views
0

我使用angularScript 2打字稿,並創建一些表格應用程序。與路由器的主要地址

我添加了路由器來導航我的應用程序。而現在,我的項目在瀏覽器中的主要地址是 - localhost。如何重定向localhostlocalhost/login。如何語法?這是我app.routing的一部分:

const appRoutes: Routes = [ 
{ component: LaskComponent, path: "table_per" }, 
{ component: DashboardComponent, path: "dashboard" }, 
{ component: LoginComponent, path: "login" }, 
{ component: RootComponent, path: "" }, 
{ component: HomeComponent, path: "home" }, 
{ component: NotFoundComponent, path: "not_found" }, 
{ path: "**", redirectTo: "not_found" }, 
]; 

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes); 

我的意思是,我將推出localhost/login當我啓動應用程序,而不是localhost

+1

您可以添加路線{路徑: '',redirectTo: '/登錄'}。路徑:''表示'localhost',或者你可以說url的根。 –

+1

這實際上是一個基本問題,你應該閱讀文檔或在Gitter上詢問。快速回答:https://angular.io/docs/ts/latest/guide/router.html#fetch-data-before-navigating並查找文件app/crisis-center/crisis-center-routing.module.ts (解決)。有一個例子 – Maxime

+1

@Maxime我添加了這個:'{path:「」,pathMatch:「full」,redirectTo:「/ login」}',就像一個例子,並且nothings的作品。我的根仍然是'本地主機' –

回答

1
const appRoutes: Routes = [ 
{ component: LaskComponent, path: "table_per" }, 
{ component: DashboardComponent, path: "dashboard" }, 
{ component: LoginComponent, path: "login" }, 
{ component: RootComponent, path: "" }, 
{ component: HomeComponent, path: "home" }, 
{ component: NotFoundComponent, path: "not_found" }, 
{ path: "**", redirectTo: "not_found" }, 
]; 

當你趕上{ component: RootComponent, path: "" }你會被加載RootComponent當網址/

取而代之的是,嘗試:

const routes: Routes = [ 
    { 
    path: '', 
    pathMatch: 'full', 
    redirectTo: '/login' 
    }, 

    { component: LaskComponent, path: 'table_per' }, 
    { component: DashboardComponent, path: 'dashboard' }, 
    { component: LoginComponent, path: 'login' }, 
    { component: HomeComponent, path: 'home' }, 
    { component: NotFoundComponent, path: 'not_found' }, 
    { path: '**', redirectTo: 'not_found' } 
]; 
+0

謝謝你,工作。 –

0

空路徑路由都需要pathMatch: 'full'時,他們沒有孩子路由或沒有redirectTo

{ component: RootComponent, path: "", pathMatch: "full"}, 

{ path: "", redirectTo: "/login}, 

確保您將<base href="/">設置爲<head>元素中的第一個元素,並確保您使用的服務器支持HTML5 pushState。

+0

謝謝你qiick的回答! –