2017-04-11 16 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标记中的子目录。

相关问题