无法解决[...]的所有参数

时间:2016-10-04 11:43:26

标签: angular typescript ionic-framework

看起来像一个非常常见的问题。我想在Service2中使用Service1,我得到了异常

  

错误:无法解析ILIASRestProvider的所有参数:( Http,?)。

即使在其他组件中Service2似乎是可注射的。而且我认为我把所有东西放在一起:

  • @Injectable at Service1 and Service2
  • tsconfig.json emitDecoratorMetadata设置为true
  • 在ngModule的提供者部分注册了Service1和Service2

如果重要的话:我正在构建一个Ionic 2 RC0应用程序。以下是重要文件:

app.module.ts

import { NgModule } from '@angular/core';
import { IonicApp, IonicModule } from 'ionic-angular';
import { MyApp } from './app.component';
import {AppConfig} from "../config/app-config";
import {ConnectionService} from "../services/ilias-app.service";
import {ILIASRestProvider} from "../providers/ilias-rest.provider";
import {MigrationsService} from "../services/migrations.service";
import {FooterToolbarService} from "../services/footer-toolbar.service";
import {ObjectListPage} from "../pages/object-list/object-list";
import {FavoritesPage} from "../pages/favorites/favorites";
import {NewObjectsPage} from "../pages/new-objects/new-objects";
import {SettingsPage} from "../pages/settings/settings";
import {InfoPage} from "../pages/info/info";
import {LogoutPage} from "../pages/logout/logout";
import {SynchronizationService} from "../services/synchronization.service";
import {DataProvider} from "../providers/data-provider.provider";
import {FileService} from "../services/file.service";
import {DataProviderFileObjectHandler} from "../providers/handlers/file-object-handler";
import {TranslatePipe} from "ionic-angular/index";
import {FileSizePipe} from "../pipes/fileSize.pipe";
import {HttpModule, Http} from '@angular/http';
import {TranslateModule} from 'ng2-translate/ng2-translate';
import {TranslateLoader} from "ng2-translate/src/translate.service";
import {TranslateStaticLoader} from "ng2-translate/src/translate.service";

@NgModule({
  declarations: [
    MyApp,
    ObjectListPage,
    FavoritesPage,
    NewObjectsPage,
    SettingsPage,
    InfoPage,
    LogoutPage,
    FileSizePipe,
    TranslatePipe
  ],
  imports: [
    HttpModule,
    IonicModule.forRoot(MyApp, {prodMode: window.hasOwnProperty('cordova')}),
    TranslateModule.forRoot({
      provide: TranslateLoader,
      useFactory: (http: Http) => new TranslateStaticLoader(http, '/assets/i18n', '.json'),
      deps: [Http]
    })
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    ObjectListPage,
    FavoritesPage,
    NewObjectsPage,
    SettingsPage,
    InfoPage,
    LogoutPage
  ],
  providers: [
    AppConfig,
    ConnectionService, //<-----------------------
    MigrationsService,
    ILIASRestProvider, //<-------------------------
    FooterToolbarService,
    DataProvider,
    FileService,
    SynchronizationService,
    DataProviderFileObjectHandler
  ]
})
export class AppModule {}

ILIAS-rest.provider.ts

@Injectable()
export class ILIASRestProvider {

    public constructor(private http:Http, public app:ConnectionService) {
    }
    ...
}

ILIAS-app.service.ts

@Injectable()
export class ConnectionService {

    ...

    constructor(public _config:AppConfig) {
        this._database = SQLiteDatabaseService.instance();
    }

    ...
}

APP-config.ts

@Injectable()
export class AppConfig {

    protected data:any = {};

    constructor(public http:Http) {
    }

    ...
}

离子设置

Your system information:

Cordova CLI: 5.4.1
Ionic Framework Version: 2.0.0-rc.0
Ionic CLI Version: 2.1.0
Ionic App Lib Version: 2.1.0-beta.1
OS: Distributor ID: Ubuntu Description: Ubuntu 14.04.5 LTS 
Node Version: v6.6.0

2 个答案:

答案 0 :(得分:2)

我建议的第一件事是直接从文件导入而不是桶,因为这是理想的方式。

import {HttpModule, Http} from '@angular/http'; 
import {ConnectionService} from "../services/ilias-app.service";

@Injectable()
export class ILIASRestProvider {

public constructor(private http:Http, public app:ConnectionService) {
}
...
}

如果您的项目无法使用上述解决方案,那么请重新排序import语句。它们的顺序不正确。例如,ILIASRestProvider中使用的http应该在ILIASRestProvider之前导入。

希望这有帮助

答案 1 :(得分:1)

在工作中遇到同样的问题。问题是我们有一个循环文件​​依赖。

  1. SignInComponent正在单元测试中进行测试
  2. SignInComponent使用了AuthService
  3. AuthService使用Routes.ts
  4. Routes.ts定义了一个以SignInComponent作为其组件的路由
  5. 当注射系统无法为您提供无法创建实例的名称时,您可以发现这是问题,只需将名称命名为“?”