获取错误“无法解析ConfigService的所有参数”

时间:2017-04-19 10:18:21

标签: angular typescript

运行Angular 2(v4)应用程序时,我似乎遇到以下错误。我似乎无法弄清楚导致这种情况的原因是@Injectable

vendor.dll.js:48569 Uncaught Error: Can't resolve all parameters for ConfigService: (?).
    at syntaxError (http://localhost:8080/vendor.dll.js:35507:34) [<root>]
    at CompileMetadataResolver._getDependenciesMetadata (http://localhost:8080/vendor.dll.js:48343:35) [<root>]
    at CompileMetadataResolver._getTypeMetadata (http://localhost:8080/vendor.dll.js:48211:26) [<root>]
    at CompileMetadataResolver._getInjectableMetadata (http://localhost:8080/vendor.dll.js:48197:21) [<root>]
    at CompileMetadataResolver.getProviderMetadata (http://localhost:8080/vendor.dll.js:48486:40) [<root>]
    at http://localhost:8080/vendor.dll.js:48414:49 [<root>]
    at Array.forEach (native) [<root>]
    at CompileMetadataResolver._getProvidersMetadata (http://localhost:8080/vendor.dll.js:48377:19) [<root>]
    at CompileMetadataResolver.getNgModuleMetadata (http://localhost:8080/vendor.dll.js:48033:50) [<root>]
    at CompileMetadataResolver.getNgModuleSummary (http://localhost:8080/vendor.dll.js:47887:52) [<root>]
    at http://localhost:8080/vendor.dll.js:47960:72 [<root>]
    at Array.forEach (native) [<root>]
    at CompileMetadataResolver.getNgModuleMetadata (http://localhost:8080/vendor.dll.js:47945:49) [<root>]
    at CompileMetadataResolver.getNgModuleSummary (http://localhost:8080/vendor.dll.js:47887:52) [<root>]

config.service.ts

import { Injectable } from '@angular/core';
import { ModuleConfig } from './';

@Injectable()
export class ConfigService {
    CONFIG_OPTIONS: ModuleConfig;

    constructor(moduleConfig?: ModuleConfig) {
        this.CONFIG_OPTIONS = new ModuleConfig();
        this.setProperty('sortIcon', moduleConfig);
        this.setProperty('sortAscIcon', moduleConfig);
        this.setProperty('sortDescIcon', moduleConfig);
        this.setProperty('sortIconSelector', moduleConfig);
        this.setProperty('i18nEnabled', moduleConfig);
        this.setProperty('defaultI18nLocation', moduleConfig);
        this.setProperty('defaultI18nLang', moduleConfig);
        this.setProperty('noi18nMessage', moduleConfig);
    }

    getConfig(): ModuleConfig {
        return this.CONFIG_OPTIONS;
    }

    private setProperty(property, moduleConfig) {
        this.CONFIG_OPTIONS[property] = (moduleConfig && moduleConfig[property]) ? moduleConfig[property] : this.CONFIG_OPTIONS[property];
    }

}

config.js

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

@Injectable()
export class ModuleConfig {
    sortIcon? = 'fa-sort';
    sortAscIcon? = 'fa-sort-asc';
    sortDescIcon? = 'fa-sort-desc';
    sortIconSelector? = 'span.fa';
    i18nEnabled? = false;
    defaultI18nLocation? = 'global';
    defaultI18nLang? = 'en';
    noi18nMessage? = 'translation-not-found';
}

@NgModule如下,

import {
    ModuleConfig,
    ConfigService
} from './';

export const MODULE_CONFIG: ModuleConfig = {
    i18nEnabled: true,
    defaultI18nLang: 'en'
};

@NgModule({
    imports: [
    ],
    declarations: [
    ],
    providers: [
        {
            provide: ModuleConfig, useValue: MODULE_CONFIG
        },
        ConfigService,
        Title
    ],
    exports: [
    ]
})
export class SharedCommonModule {}

1 个答案:

答案 0 :(得分:3)

我认为Angulars DI不喜欢

中的?
constructor(moduleConfig?: ModuleConfig) {

我会在没有

的情况下尝试
constructor(moduleConfig: ModuleConfig) {

或者

constructor(@Inject(ModuleConfig) moduleConfig?: ModuleConfig) {