【发布时间】:2017-02-11 07:04:33
【问题描述】:
我正在尝试将我的 Ionic2.beta11 应用程序移植到新的 Ionic2.rc0 版本。大多数事情都很简单,但我的 AoT 编译器有问题。
我有一个 AuthService:
@Injectable()
export class AuthService {
constructor(@Inject(Http) http: Http) {
this.http = http;
}
...
}
我将它注入到我的应用程序中的src/app/app.module.ts 文件中:
@NgModule({
declarations: [
MyApp,
...
],
imports: [
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
...
],
providers: [
AuthService
]
})
运行 ionic serve 时一切正常,但是当我尝试构建它时,出现以下错误:
ngc error: Error: Error at .../.tmp/app/app.module.ngfactory.ts:397:78: Supplied parameters do not match any signature of call target.
第 396 - 399 行:
get _AuthService_74():import44.AuthService {
if ((this.__AuthService_74 == (null as any))) { (this.__AuthService_74 = new import44.AuthService()); }
return this.__AuthService_74;
}
问题是new import44.AuthService() 需要一个 http 类型的参数。
有趣的是,当我在定义文件中手动将constructor(http: Http) 替换为constructor() 时,一切正常。
我阅读了我能找到的所有 StackOverflow 答案,但没有一个解决方案能解决我的问题。
我是否需要更改 AuthService 中的构造函数或将其注入应用程序的方式?感谢您的帮助。
【问题讨论】:
-
作为临时解决方案,我将参数设为可选,现在可以使用。但我仍在寻找合适的解决方案。
标签: angular typescript dependency-injection ionic2