如何在angular2中为不同的环境实例使用不同的base_url

时间:2017-04-25 17:46:56

标签: angular angular2-routing

我对angular2的index.html文件有以下内容:

我想要的是,当我在(npm run build)默认模式(开发模式)中构建它时,它应该是

<base href="/">

但是当我在生产模式下构建它时(npm run build --target = production),它应该是

<base href="/new/">

我是angular1的专家,但对angular2来说是新手,但是我试图在index.ts文件中为base_url添加一个变量,但这似乎并没有起作用。

我希望有一种解决这个问题的快捷方法,任何人都可以在这里帮忙。

2 个答案:

答案 0 :(得分:2)

您可以使用APP_BASE_HREF提供商设置其值

@NgModule({
  imports: [..],
  declarations: [..],
  providers: [{provide: APP_BASE_HREF, useValue: '/new/'}]
})
class AppModule {}

答案 1 :(得分:2)

因为您使用的是Angular CLI,所以它确实支持

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

您可以更新package.json中的脚本:

"scripts": {
  //...
  "start": "ng serve -bh /anotheUrl/",
  "build": "ng build -bh /myUrl/",
  //..
},

https://github.com/angular/angular-cli/wiki/build