Angular2-错误:无法解析IconService的所有参数 [英] Angular2 - Error: Can't resolve all parameters for IconService

查看:71
本文介绍了Angular2-错误:无法解析IconService的所有参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图将我的应用切换到AoT编译,并且在加载应用时在生产环境中遇到此错误(它在本地运行良好).

I've been trying to switch my app over to AoT compilation and have been getting this error in the production environment when the app is loading (it works fine locally).

Error: Can't resolve all parameters for IconService: (?, ?)

似乎错误来自提供IconService的模块上.图标服务的构造函数看起来像

it seems like the error is coming from on the modules that is providing the IconService. The icons services constructor looks like

constructor(private http:Http, private iconConfiguror:IconConfiguror) {

所以我的问题是这个错误是什么意思,为什么它只会在生产环境中发生(我尝试过在本地启用生产模式)?

So my question is what does this error mean and why would it happen in the prod environment only (I've tried enabling prod mode locally)?

似乎没有提供http和icon配置参数,但是在应用程序模块级别提供了icon config,并且在提供IconServiceIconModule中导入了HttpModule

It seems like it means that the http and icon configuration parameters aren't provided, but the icon config is provided at the app module level and the HttpModule is imported in the IconModule where the IconService is provided.

@NgModule({
    imports: [
        CommonModule,
        HttpModule,
    ],
    declarations: [
        IconComponent,
    ],
    exports: [
        IconComponent,
    ],
    providers: [
        IconService,
        __platform_browser_private__.BROWSER_SANITIZATION_PROVIDERS,
    ],
})

还有图标组件的枪管.

export * from "./components/icon/icon.configuror";

export * from "./components/icon/icon.service.provider";

export * from "./components/icon/icon.service";

export * from "./components/icon/icon.component";

export * from "./components/icon/icon.module";

推荐答案

通过以其他方式提供IconService来解决此问题.

Fixed this by providing the IconService in a different way.

    {
        provide: IconService,
        useFactory: iconServiceFactory,
        deps: [Http, IconConfiguror],
    },

还有工厂本身

export function iconServiceFactory(http: Http, iconConfiguror: IconConfiguror) {
    return new IconService(http, iconConfiguror);
}

我猜测由于某种原因未提供Http(即使已导入HttpModule),所以我不得不将其声明为依赖项.

I guess for some reason the Http wasn't being provided (even though HttpModule was imported) so I had to declare it as a dependency.

这篇关于Angular2-错误:无法解析IconService的所有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆