2017-10-28 150 views
0

我在我的應用中遇到路由問題。在我的本地所有作品中,但不在服務器上。該程序有以下路線:Angular 4中的路由選擇

const appRoutes: Routes = [ 
    { path: '', component: HomeComponent }, 
    { path: 'login', component: LoginComponent }, 
    { path: 'register', component: RegisterComponent }, 
    { path: 'thankyou', component: ThankyouComponent }, 
    { path: 'home', component: UserHomeComponent, canActivate: [AuthGuard] }, 
    { path: 'publish', component: PublishComponent, canActivate: [AuthGuard] }, 
    { path: '**', component: PageNotFoundComponent } 
]; 

root route始終有效。

如果我的電腦上我直接發送請求,例如,路線:

http://localhost:4200/publish 

假設我已經登錄不存在任何問題加載它。但是,如果在服務器中使用路由執行該路由:

http://myserver/mypath/dist/publish 

找不到路由。

我修改了index.html,以便在服務器上執行。

<base href="/">通過<base href="/mypath/dist/">

如果我使用指令

routerLink="/publish" 

它工作正常執行由HTML模板這條路。

有誰知道爲什麼會發生這種情況?

+0

你在哪裏定義了'myserver/mypath/dist /'?我沒有看到你的問題。 –

+0

該路線是我擁有投影源的服務器路徑 – cointreau17

+0

你會發布處理傳入路線的代碼嗎? –

回答

-1

您的網絡服務器需要配置爲將請求重新路由到您的index.html,不包括資產。

對於nginx的,添加此您nginx.conf的服務器塊或您的網站的配置裏面:

location/{ 
    try_files $uri $uri/ /path/to/index.html; 
} 

對於Apache,檢查this answer

+0

我會看看答案,並告訴你它是否有效,謝謝! – cointreau17

0

這裏是一個文件的例子,它創建了一個路由結構,以便在路由被放入url時被命中。到目前爲止,我還沒有在你的問題中看到任何暗示你實際上在你的應用程序之外處理路線的事情。換句話說,如果你還沒有定義一個可以作爲url輸入的路由,你的服務器的某個url如何知道去哪裏?似乎你沒有根路徑。這就是我一直在駕駛的。

import { Routes, RouterModule } from '@angular/router'; 
import { SomeComponent } from 'app/components/some-component'; 
import { Path1Component } from 'app/components/path1-component'; 
import { Path2Component } from 'app/components/path2-component'; 
import { Path3Component } from 'app/components/path3-component'; 

const MyRoutes: Routes = [ 
    { 
     path: 'root/', 
     component: SomeComponent, 
     children: [ 
      { 
       path: 'path1', 
       component: Path1Component 
      }, 
      { 
       path: 'path2', 
       component: Path2Component 
      }, 
      { 
       path: 'path3', 
       component: Path3Component 
      }, 
      { 
       path: '**',   // default path 
       component: Path2Component 
      } 
     ], 
    } 
]; 

@NgModule({ 
    imports: [ 
     RouterModule.forChild(MyRoutes) 
    ], 
    exports: [ 
     RouterModule 
    ] 
}) 

export class MyRouting { } 
+0

現在我明白了。我認爲像根路徑重定向{path:'',component:HomeComponent},「/ login」路徑可以重定向{path:'login',component:LoginComponent}。我會嘗試這種方式,我會告訴你它是否有效。謝謝! – cointreau17

+0

嗨,我有學習關於路由器[鏈接](https://angular.io/guide/router)的角度指南和執行英雄示例在服務器的代碼它發生同樣的問題,例如,點擊Crisis Center它重定向 – cointreau17

+0

對不起以前的答案,我想寫這個:嗨,我一直在閱讀關於路由器[鏈接](https://angular.io/guide/router)的Angular指南,並執行服務器中英雄示例的簡單代碼,發生同樣的問題。 – cointreau17