Set base href from an environment variable with ng build

后端 未结 1 1058
一生所求
一生所求 2021-02-03 22:21

Does anyone know how to accomplish this with the angular-cli? I would like to be able to store the baseHref path in an environment variable within /src/envir

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-03 22:44

    You would have to use APP_BASE_HREF

    @NgModule({
      providers: [{provide: APP_BASE_HREF, useValue: environment.baseHref }]
    })
    class AppModule {}
    

    See angular doc

    EDIT

    Since CSS/JS does not work with APP_BASE_HREF, you can do this:

    In app.component.ts, inject DOCUMENT via import {DOCUMENT} from "@angular/platform-browser";

    constructor(@Inject(DOCUMENT) private document) {
    }
    

    Then on your ngOnInit()

    ngOnInit(): void {
        let bases = this.document.getElementsByTagName('base');
    
        if (bases.length > 0) {
          bases[0].setAttribute('href', environment.baseHref);
    
        }
      }
    

    0 讨论(0)
提交回复
热议问题