typescript Angular2 - 错误:无法解析 IconService 的所有参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42425139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular2 - Error: Can't resolve all parameters for IconService
提问by tallkid24
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).
我一直在尝试将我的应用程序切换到 AoT 编译,并且在加载应用程序时在生产环境中遇到此错误(它在本地工作正常)。
Error: Can't resolve all parameters for IconService: (?, ?)
Error: Can't resolve all parameters for IconService: (?, ?)
it seems like the error is coming from on the modules that is providing the IconService. The icons services constructor looks like
错误似乎来自提供 IconService 的模块。图标服务构造函数看起来像
constructor(private http:Http, private iconConfiguror:IconConfiguror) {
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)?
所以我的问题是这个错误是什么意思,为什么它只会在 prod 环境中发生(我已经尝试在本地启用 prod 模式)?
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.
这似乎意味着没有提供 http 和 icon 配置参数,但是 icon 配置是在应用程序模块级别提供的,并且在提供HttpModule
的IconModule
地方导入IconService
。
@NgModule({
imports: [
CommonModule,
HttpModule,
],
declarations: [
IconComponent,
],
exports: [
IconComponent,
],
providers: [
IconService,
__platform_browser_private__.BROWSER_SANITIZATION_PROVIDERS,
],
})
And the barrel for our icon component.
还有我们图标组件的桶。
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";
回答by tallkid24
Fixed this by providing the IconService in a different way.
通过以不同的方式提供 IconService 解决了这个问题。
{
provide: IconService,
useFactory: iconServiceFactory,
deps: [Http, IconConfiguror],
},
and the factory itself
和工厂本身
export function iconServiceFactory(http: Http, iconConfiguror: IconConfiguror) {
return new IconService(http, iconConfiguror);
}
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.
我想由于某种原因没有提供 Http (即使 HttpModule 已导入),所以我不得不将其声明为依赖项。
回答by Mark Amery
One possible cause of this error is if you are not decorating your IconService
class with @Injectable()
. If that is the cause, adding that decoration above the class declaration will fix the error.
此错误的一个可能原因是,如果您没有IconService
使用@Injectable()
. 如果这是原因,在类声明上方添加该装饰将修复错误。
回答by LoganMzz
I have encountered similar problem. I solved it by changing export order in the barrel.
我遇到过类似的问题。我通过改变桶中的出口顺序解决了这个问题。
Basic service files:
基本服务文件:
// dependency.service.ts
@Injectable()
export class DependencyService { }
// dependant.service.ts
import { DependencyService } from '.';
@Injectable()
export class DependantService {
constructor(private dependency: DependencyService) { }
}
Following barrel causes the error:
以下桶导致错误:
// index.ts
export * from './dependant.service';
export * from './dependency.service';
While following one works:
在以下一项工作时:
// index.ts
export * from './dependency.service';
export * from './dependant.service';
回答by Alex Maximenko
Sometimes is single way to fix it - manually describe parameters.
有时是修复它的单一方法 - 手动描述参数。
static get parameters() { return [Http, IconConfiguror] }
constructor(private http:Http, private iconConfiguror:IconConfiguror) {
回答by Rakesh
Worked for me when I used following while importing the Service in app.module.ts
当我在 app.module.ts 中导入服务时使用以下内容时对我有用
{provide: AuthService,
depends: HttpClientModule}
回答by tone
My issue was that I was inheriting from a base class, and I had decorated that base class with @Injectable. The inheriting class was the class that should have the @Injectable attribute, not the base class. It seems that when the compiler sees the @Injectable attibute, it checks to make sure all the properties in the constructor can be injected. If not, it's an error. I solved it by removing the @Injectable attibute from that class.
我的问题是我是从一个基类继承的,我用@Injectable 装饰了那个基类。继承类是应该具有@Injectable 属性的类,而不是基类。似乎当编译器看到 @Injectable 属性时,它会检查以确保可以注入构造函数中的所有属性。如果没有,那就是错误。我通过从该类中删除 @Injectable 属性来解决它。