2017-04-11 17 views
1

我的Angular2應用程序部署在IIS上網站的子目錄中。我使用下面的命令來構建它。 (--bh設置子目錄作爲根的應用):當路由到應用程序的子目錄時,URL在Angular2中增長

ng build --bh testdna --no-aot --dev 

我然後在應用程序中使用的路由與如下

app.routes.ts:

import { Routes } from '@angular/router'; 
import { DnaPlotComponent } from './dna-plot/dna-plot.component'; 

export const routes: Routes = [{ path: '', component: DnaPlotComponent }]; 

app.module.ts:

import { RouterModule } from '@angular/router'; 
import { routes } from './app.routes'; 
... 
RouterModule.forRoot(routes) 

每次頁面刷新的URL有時間附加到它的子目錄。這意味着URL會無限增長,無法刷新,導致404錯誤。下面是我看到的一個例子。

https://develop.domain.com/dnaplot/dnaplot#/dnaplot/ 

原始URL稱爲是:

https://develop.domain.com/dnaplot 

任何想法,以制止這種情況的發生,將不勝感激。

回答

2

您需要設置<base href="...">或提供指向已部署路徑的APP_BASE_HREF

欲瞭解更多詳情,請參閱Angular 2 router no base href set

提供APP_BASE_HREF通常是更好的方法,因爲它僅影響路由器,而<base href="...">影響,如SVG引用其他的東西。

與空路徑''並沒有子路由的路由應該有pathMatch: 'full'

export const routes: Routes = [{ path: '', component: DnaPlotComponent , pathMatch: 'full'}]; 
0

最終的解決方案是簡單地基本href設置爲空字符串,即。 <base href="">

路由器似乎在原始構建命令中'知道'--bh testdna,因此不需要重新聲明href標記中的子目錄。

相關問題