2016-09-23 188 views
4

當我轉移到Angular-CLI時,我剛剛從systemjs移到了webpack。 它曾經工作,但它不再。 以下是我有:使用webpack動態鏈接基礎href鏈接

在我的index.html在

<script type='text/javascript'> 
    var base = document.location.pathname.split('/')[1]; 
    document.write('<base href="/' + base + '" />'); 
</script> 

而且我還添加了一個「/」,在爲數不多的js文件的根目錄添加波紋管。

既然我在webpack上,那些文件是從其他地方加載的,我不能再添加這個「/」。 因此,試圖加載它們http://www.exemple.com/it/xxx.js,而不是http://www.exemple.com/xxx.js

我已經看到了,我必須更新webpack.config.js文件的東西,但角CLI傢伙已經決定把它放在自己的模塊中,而不是像往常一樣在項目根目錄。 我知道我可以編輯這個文件,但我不想每次更新angular-cli時再次執行此操作。

有沒有很好的方法來做到這一點?

回答

4

建設時,您可以使用--base-href your-url選項修改 index.html中的基本標記()。

# Sets base tag href to /myUrl/ in your index.html 
ng build --base-href /myUrl/ 
ng build --bh /myUrl/ 

https://github.com/angular/angular-cli#base-tag-handling-in-indexhtml

+0

感謝您的時間!是的,我已經看到了,但我希望所有縣都有1個源代碼,並且我覺得我可以建立--bh/it /',我將成爲這個國家的獨家代理。我錯了嗎 ? – Tom

+0

我們有同樣的問題,我們目前解決它的方式是在部署應用程序時使用python腳本更新base-href。 – shusson

1

也許你可以使用供應商在你的@NgModule註釋:

@NgModule({ 
     declarations: [ 
      AppComponent,... ], 
     providers: [ 
      { provide: APP_BASE_HREF, useValue: '/'},... ], 
     bootstrap: [AppComponent] 
}) 
export class AppModule { } 
+0

我不知道這一個。謝謝!我會看看。 – Tom