Angular 在 forRoot 方法中调用函数
Angular call function inside forRoot method
问题是我在 forRoot 方法中调用一个函数,如下所示:
app.module.ts
import {environment} from '../environments/environment';
...
@NgModule({
imports: [
BrowserModule,
MyModule.forRoot({
config: {
sentryURL: environment.SENTRY_URL <-- This, calls the function
}
}),
HttpClientModule,
...
]})
environemnt.ts
export function loadJSON(filePath) {
const json = loadTextFileAjaxSync(filePath, 'application/json');
return JSON.parse(json);
}
export function loadTextFileAjaxSync(filePath, mimeType) {
const xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', filePath, false);
if (mimeType != null) {
if (xmlhttp.overrideMimeType) {
xmlhttp.overrideMimeType(mimeType);
}
}
xmlhttp.send();
if (xmlhttp.status === 200) {
return xmlhttp.responseText;
} else {
return null;
}
}
export const environment = loadJSON('/assets/config.json');
配置如下所示:
{
"production": "false",
"SENTRY_URL": "https://...@sentry.com/whatever/1"
}
当我使用 aot 构建时,它显示:
ERROR in src/app/app.module.ts(41,20): Error during template compile of 'AppModule'
Function calls are not supported in decorators but 'loadJSON' was called in 'environment'
'environment' calls 'loadJSON'.
有什么想法吗??
:)
更新的解决方案:
我的最终解决方案是,在应用程序中,使用 Suren Srapyan 所说的函数 getter。在库中,forRoot 方法应该如下所示:
export const OPTIONS = new InjectionToken<string>('OPTIONS');
export interface MyModuleOptions {
config: {
sentryURLGetter: () => string | Promise<string>;
}
}
export function initialize(options: any) {
console.log('sentryURL', options.config.sentryURLGetter());
return function () {
};
}
@NgModule({
imports: [
CommonModule
]
})
export class MyModule {
static forRoot(options: MyModuleOptions): ModuleWithProviders {
return {
ngModule: MyModule,
providers: [
{provide: OPTIONS, useValue: options},
{
provide: APP_INITIALIZER,
useFactory: initialize,
deps: [OPTIONS],
multi: true
}
]
};
}
}
:D
@Decorators
不支持函数调用。或者,您可以在 @NgModule
之外获取值,而不是使用它的值。
export function getSentryUrl() {
return environment.SENTRY_URL;
}
@NgModule({
imports: [
BrowserModule,
MyModule.forRoot({
config: {
getSentryURL: getSentryUrl
}
}),
HttpClientModule,
...
]})
问题是我在 forRoot 方法中调用一个函数,如下所示:
app.module.ts
import {environment} from '../environments/environment';
...
@NgModule({
imports: [
BrowserModule,
MyModule.forRoot({
config: {
sentryURL: environment.SENTRY_URL <-- This, calls the function
}
}),
HttpClientModule,
...
]})
environemnt.ts
export function loadJSON(filePath) {
const json = loadTextFileAjaxSync(filePath, 'application/json');
return JSON.parse(json);
}
export function loadTextFileAjaxSync(filePath, mimeType) {
const xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', filePath, false);
if (mimeType != null) {
if (xmlhttp.overrideMimeType) {
xmlhttp.overrideMimeType(mimeType);
}
}
xmlhttp.send();
if (xmlhttp.status === 200) {
return xmlhttp.responseText;
} else {
return null;
}
}
export const environment = loadJSON('/assets/config.json');
配置如下所示:
{
"production": "false",
"SENTRY_URL": "https://...@sentry.com/whatever/1"
}
当我使用 aot 构建时,它显示:
ERROR in src/app/app.module.ts(41,20): Error during template compile of 'AppModule' Function calls are not supported in decorators but 'loadJSON' was called in 'environment' 'environment' calls 'loadJSON'.
有什么想法吗??
:)
更新的解决方案:
我的最终解决方案是,在应用程序中,使用 Suren Srapyan 所说的函数 getter。在库中,forRoot 方法应该如下所示:
export const OPTIONS = new InjectionToken<string>('OPTIONS');
export interface MyModuleOptions {
config: {
sentryURLGetter: () => string | Promise<string>;
}
}
export function initialize(options: any) {
console.log('sentryURL', options.config.sentryURLGetter());
return function () {
};
}
@NgModule({
imports: [
CommonModule
]
})
export class MyModule {
static forRoot(options: MyModuleOptions): ModuleWithProviders {
return {
ngModule: MyModule,
providers: [
{provide: OPTIONS, useValue: options},
{
provide: APP_INITIALIZER,
useFactory: initialize,
deps: [OPTIONS],
multi: true
}
]
};
}
}
:D
@Decorators
不支持函数调用。或者,您可以在 @NgModule
之外获取值,而不是使用它的值。
export function getSentryUrl() {
return environment.SENTRY_URL;
}
@NgModule({
imports: [
BrowserModule,
MyModule.forRoot({
config: {
getSentryURL: getSentryUrl
}
}),
HttpClientModule,
...
]})