【发布时间】:2017-03-17 18:01:19
【问题描述】:
我使用的是 Ionic 2,但自 2.0.0 版以来,提供程序/注入存在一些问题。 我不断收到的错误如下(使用不同的提供者,而不仅仅是这个):
未捕获的错误: 无法解析 LoginRedirectService 的所有参数:(App, ?, AlertController)。
解决我的提供程序的注入似乎存在问题,即使它们都用@Injectable 装饰。
一个文件
@Injectable()
export class LoginRedirectService {
constructor(
public app:App,
//another provider, needed within this one - this cannot be resolved
public authService: AuthenticationService,
public alert:AlertController
)
//...
}
不同的文件
@Injectable()
export class AuthenticationService {
constructor(public dataService:DataService){
//...
}
}
//...
等等。
我还在 app.module.ts 中添加了所有提供程序,按照它们必须注入的顺序:
//Ordered by dependencies, bottom is the most dependent on others
providers: [
HttpModule,
JsonpModule,
RssService,
EmergencyEmailService,
InAppBrowserService,
ExternalBrowser,
AppLauncherService,
LauncherPlaner,
LauncherSkype,
ConnectivityService,
MockRemoteDataService,
SoniaMirthRemoteDataService,
DataService,
AuthenticationService,
LoginRedirectService,
NewsStreamService,
SoniaBootstrapService
]
我的项目结构如下:
- 源
- 应用程序
- app.module.ts
- app.component.ts
- 资产
- 页
- 供应商
- 子文件夹
- ...
- 应用程序
- package.json 等
我尝试在需要它们的类的构造函数中显式调用@Inject,但也没有成功。
我真的不知道该怎么做,感谢任何帮助,谢谢
【问题讨论】:
-
AuthenticationService或DataService是否依赖于Http?HttpModule和JsonpModule应该进入@NgModule.imports而不是providers。如果您在imports中还没有它,并且服务 依赖于Http,那会 导致此错误 -
谢谢,我确实从那里删除了它们,但它们已经添加到导入中,但并没有解决问题。
-
我遇到了同样的问题。在我的情况下,一个提供者正在导入另一个,尽管这似乎不是这里的情况。只是我的两分钱,以防其他人像我一样在这里碰到:)
标签: angular typescript ionic-framework ionic2 angular2-providers