简体   繁体   English

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

[英]Can't resolve all parameters for [...]

Looks like a pretty common problem.看起来是一个很常见的问题。 I want to use Service1 in Service2 and I get the exception我想在 Service2 中使用 Service1,但出现异常

Error: Can't resolve all parameters for ILIASRestProvider: (Http, ?).错误:无法解析 ILIASRestProvider 的所有参数:(Http, ?)。

even though in other Components Service2 seems to be injectable.即使在其他组件中 Service2 似乎是可注入的。 And I think I have everything together:我想我把一切都放在了一起:

  • @Injectable at the Service1 and Service2 @Injectable 在 Service1 和 Service2
  • tsconfig.json emitDecoratorMetadata set to true tsconfig.json 发射装饰器元数据设置为 true
  • Registered Service1 and Service2 in the Providers part of the ngModule在 ngModule 的 Providers 部分注册了 Service1 和 Service2

If it matters at all: I'm building an Ionic 2 RC0 Application.如果这很重要:我正在构建一个 Ionic 2 RC0 应用程序。 Here are the important files:以下是重要文件:

app.module.ts 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 ilias-rest.provider.ts

@Injectable()
export class ILIASRestProvider {

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

ilias-app.service.ts ilias-app.service.ts

@Injectable()
export class ConnectionService {

    ...

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

    ...
}

app-config.ts app-config.ts

@Injectable()
export class AppConfig {

    protected data:any = {};

    constructor(public http:Http) {
    }

    ...
}

ionic setup离子设置

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

First thing i would suggest is to import from files directly rather than barrels, because that is the ideal way.我建议的第一件事是直接从文件而不是桶中导入,因为这是理想的方式。

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) {
}
...
}

second thing if the above solution is not possible with your project than please reorder the import statements.第二件事,如果您的项目无法使用上述解决方案,请重新排序导入语句。 They are not in correct order.它们的顺序不正确。 for example http is being used in ILIASRestProvider should be imported before ILIASRestProvider.例如,在ILIASRestProvider 中使用的http 应该在ILIASRestProvider 之前导入。

Hope this helps希望这可以帮助

Just had the same problem here at work.刚刚在工作中遇到了同样的问题。 The problem was that we had a circular file dependency.问题是我们有一个循环文件​​依赖。

  1. SignInComponent was being tested in a unit test SignInComponent 正在单元测试中进行测试
  2. SignInComponent used AuthService SignInComponent 使用了 AuthService
  3. AuthService used Routes.ts AuthService 使用 Routes.ts
  4. Routes.ts defined a route that had SignInComponent as its component Routes.ts 定义了一个以 SignInComponent 作为其组件的路由

You can spot this is the problem when the injection system cannot give you the name of what it is unable to create an instance of, and simply gives the name as "?"当注入系统无法为您提供无法创建实例的名称而只是将名称提供为“?”时,您可以发现这是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM