相关疑难解决方法(0)

VSTS构建-在发布阶段替换Angular4环境变量

目前,我在Angular4中拥有三种不同的环境:

  • 环境
  • 环境调试
  • 环境释放

在此处输入图片说明

现在,在vsts构建管道中,我设置了多重配置,其中一个定义为调试和发布准备了工件:
在此处输入图片说明

在运行npm installnpm run(调试或发布)之前,我正在使用“ 替换令牌 ”任务替换每个调试和发布环境的变量,然后运行webpack打包用于调试或发布环境的文件。

我看到Release中有一个替换变量的选项,您可以在其中替换.json文件中的变量(例如appsettings.json): 在此处输入图片说明

但是问题是当webpack将源代码打包到一个bundle.js中时,我想我不能使用release来替换环境变量吗?我可以吗?

因此,我要实现的是将调试和发布版本分离。这很简单,我只是为调试和发布重新创建了单独的定义,其中我仅在每个环境中替换变量。第二阶段是实际上从构建管道中删除“替换令牌”任务,并使用“发布变量”部分替换在“发布”中设置的每个环境的变量。webpack在js中构建包后,Angular怎么可能?

c# azure azure-devops angular

7
推荐指数
2
解决办法
4273
查看次数

AppModule加载之前的角加载外部配置

考虑以下情形(Angular v7):

  1. 在加载AppModule 之前,从外部端点(JSON)加载配置参数(API服务器URL和Auth服务器URL)
  2. 将配置传递给AppModule(OAuth2模块)
  3. 使用AOT编译应用

第二点是这里的关键,看起来像这样:

@NgModule({
  imports: [
    ...
    OAuthModule.forRoot({
      resourceServer: {
        allowedUrls: [API_SERVER_URL], // <== we need to set the value that we loaded from the external endpoint (JSON) here
        sendAccessToken: true
      }
    }),
    ...
  ],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

我已经尝试了以下内容:

  • 使用APP_INITIALIZER的解决方案。这是行不通的,因为OAuthModule.forRoot()会在APP_INITIALIZER下载外部配置JSON 之前触发。
  • 加载配置与异步功能main.ts进入角环境变量,然后引导中的AppModule。由于中的import { AppModule } from './app/app.module';语句也无法正常工作main.ts,这会导致AppModule在加载OAuthModule.forRoot()外部配置之前加载并启动(此注释确认了此行为)。
  • 加载的AppModule动态main.ts …

typescript angular

6
推荐指数
3
解决办法
1159
查看次数

APP_INITIALIZER在出厂前未触发

APP_INITIALIZER用来加载环境特定的变量。我需要在我的中使用这些变量authConfigFactory,但是工厂会APP_INITIALIZER在应用程序配置内部完成之前保持加载状态。

我正在使用这个库:https : //github.com/manfredsteyer/angular-oauth2-oidc

我想使用APP_CONFIG.authConfig.allowedUrls我的auth config工厂内部的值。我如何确保它在认证工厂之前先设置配置。

我在工厂收到此错误: Cannot read property 'authConfig' of undefined

app.module.ts

providers: [
    AppConfigService,
    {
      provide: APP_INITIALIZER,
      useFactory: (config: AppConfigService) => () => config.load(),
      multi: true,
      deps: [AppConfigService]
    },
    {
      provide: OAuthModuleConfig,
      useFactory: authConfigFactory
    }
]
Run Code Online (Sandbox Code Playgroud)

app.config.ts

export let APP_CONFIG: AppConfig;

@Injectable()
export class AppConfigService {
  constructor(
    private injector: Injector
  ) {}

  config = null;

  public load() {
    const http = this.injector.get(HttpClient);

    return http
      .get('../assets/config/config.json')
      .pipe( …
Run Code Online (Sandbox Code Playgroud)

dependency-injection typescript angular

5
推荐指数
1
解决办法
510
查看次数