简体   繁体   English

InjectionToken for config不能与AoT一起使用

[英]InjectionToken for config not working with AoT

I'm writing an Angular library where I need to pass in a configuration object in the .forRoot() of the module import. 我正在编写一个Angular库,我需要在模块导入.forRoot()传入一个配置对象

I'm trying to do this (the apparently correct way) with InjectionToken , but getting an AoT compilation error. 我正在尝试使用InjectionToken执行此操作(显然是正确的方法),但是收到AoT编译错误。

Error when compiling with AoT: 使用AoT进行编译时出错:

error TS4050: Return type of public static method from exported class has or is using name 'InjectionToken' from external module "/path/to/node_modules/@angular/core/src/di/injection_token" but cannot be named. 错误TS4050:从导出类返回的公共静态方法类型具有或正在使用来自外部模块“/ path / to / node_modules / @ angular / core / src / di / injection_token”的名称'InjectionToken',但无法命名。

Any help is appreciated. 任何帮助表示赞赏。

module.ts: module.ts:

import { NgModule } from '@angular/core';
import { CONFIG, Config } from './config';

@NgModule({ ... })
export class SomeModule {
    static forRoot(config?: Config) {
        return {
            ngModule: SomeModule,
            providers: [
                { provide: CONFIG, useValue: config }
            ]
        };
    }
}

config.ts: config.ts:

import { InjectionToken } from '@angular/core';

export const CONFIG = new InjectionToken<Config>('config');

export interface Config {
    some?: string;
    values?: string;
    here?: string;
}

Desired usage by another application: 其他应用程序所需的用法:

import { NgModule } from '@angular/core';
import { SomeModule } from 'some-library';

@NgModule({
    imports: [
        SomeModule.forRoot({
            // <-- config values here
        })
    ]
})

(Angular 5, TypeScript 2.6.2) (Angular 5,TypeScript 2.6.2)

Answer by JB Nizet in comments above... JB Nizet在上面的评论中回答......

Needed to specify the type returned by the forRoot method, ModuleWithProviders . 需要指定forRoot方法ModuleWithProviders返回的类型。

import { NgModule } from '@angular/core';
import { CONFIG, Config } from './config';

@NgModule({ ... })
export class SomeModule {
    static forRoot(config?: Config): ModuleWithProviders {
        return {
            ngModule: SomeModule,
            providers: [
                { provide: CONFIG, useValue: config }
            ]
        };
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户定义的InjectionToken的DI不支持AOT - DI with user-defined InjectionToken not working with AOT enabled Angular - InjectionToken 可以与 AOT 编译一起使用吗? - Angular - Can InjectionToken be used with AOT Compilation? 没有为InjectionToken自定义HTTP配置提供程序 - No provider for InjectionToken Custom HTTP Config 没有 InjectionToken 全局配置的提供者! 错误 - No provider for InjectionToken Global Config! error NullInjectorError: InjectionToken NGX_ECHARTS_CONFIG 没有提供者 - NullInjectorError: No provider for InjectionToken NGX_ECHARTS_CONFIG NullInjectorError: InjectionToken MSAL_GUARD_CONFIG 没有提供程序 - NullInjectorError: No provider for InjectionToken MSAL_GUARD_CONFIG 升级到webpack 4后,Angular Application中的InjectionToken Config否 - No for InjectionToken Config in Angular Application after upgrading to webpack 4 angular 业力 - NullInjectorError:没有 InjectionToken 配置的提供者 - angular karma - NullInjectorError: No provider for InjectionToken config Quill Angular 错误:NullInjectorError: No provider for InjectionToken config - Quill Angular Error: NullInjectorError: No provider for InjectionToken config MSAL Angular - 动态重定向 Url 配置通过 document.location.origin 不适用于 AOT - MSAL Angular - Dynamic Redirect Url Config via document.location.origin not working with AOT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM