2016-09-23 186 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