首页 > 解决方案 > 如何从 index.html 在 Angular 8 应用程序中动态定义“base href”?

问题描述

如何从 index.html 在 Angular 8 应用程序中动态定义“base href”?

之前我用过--base-href,现在不支持了。

标签: angularangular-clibase-urlangular-cli-v8

解决方案


您可以通过脚本设置属性并在 NgModule 中获取来动态设置基本 href。

  //index.html
    <script>
      window['base-url'] = window.location.pathname;
    </script>

在您的模块中,您需要使用

@NgModule({
  providers: [{provide: APP_BASE_HREF, useValue: window['base-url]}]
})

推荐阅读