在使用AOT的角度库配置中使用window.location.origin

问题描述 投票:0回答:1

当我在我的角度库的window.location.origin配置对象参数中传递forRoot({...})并使用AOT进行编译时,其值为null。有启用AOT的方法可以将其值传递给我的角度库吗?

经过一些谷歌搜索之后,我想也许需要使用WINDOW设置一个注入令牌,然后在库侧使用它,但是如果可能的话,我想避免这种情况。我也可以对源代码进行硬编码,但这是非常有限的。想要进行某种设置,然后忘记它的方法,因此为什么我将一个功能雕刻到一个库中。

我的书架的设置如下。

export class AppConfig {
  origin: string;
}

...

export const APP_CONFIG_TOKEN = new InjectionToken<AppConfig>('APP_CONFIG_TOKEN');

...

@NgModule()
export class MyLibrary {
  public static forRoot(config: AppConfig) {
    return {
      ngModule: MyLibrary,
      providers: [
        {
          provide: APP_CONFIG_TOKEN,
          useValue: config
        }   
      ]
    };
  }

我正在使用库中服务中的令牌的值

@Injectable()
export class MyService {
  constructor(@Inject(APP_CONFIG_TOKEN) private config: AppConfig) { }

  myOrigin(): string {
    return this.config.origin; // expecting it to return a value like `http://localhost:4200` but it returns `null`
  }
}

并且我正在像这样将库导入到AppModule中>]

@NgModule({
  imports: [
    MyLibrary.forRoot({ 
      origin: window.location.origin // with AOT, this seems to be null
    }) 
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

当我在我的角度库的forRoot({...})配置对象参数中传递window.location.origin并使用AOT进行编译时,其值为null。有没有办法将其值传递给我的...

javascript angular aot angular-library
1个回答
0
投票

AOT会构建应用程序的各个部分,以便客户端不必进行更多的计算。尝试看这个问题

© www.soinside.com 2019 - 2024. All rights reserved.