Angular 2 - Injectable Service需要另一个可注入服务 - Uncaught无法解析所有参数

时间:2016-05-25 16:49:17

标签: javascript angularjs dependency-injection

注入服务和Angular 2时出现问题。当我在chrome中启动应用程序时,我在控制台中出现以下错误:

  

reflective_provider.js:232 Uncaught无法解析所有参数   ' User'(LocalDataBase,Platform,undefined)。确保所有的   参数使用Inject进行修饰或具有有效的类型注释   那个'用户'用Injectable装饰。

方案如下:

我的主App.ts呼叫服务User.ts,User.ts呼叫2个服务:

App.ts
|
|--- Users.ts
     |
     |---localDataBase.ts
     |---DataServer.ts (service with http in constructor)

在我的app.ts中,我添加:

providers: [HTTP_PROVIDERS,LocalDataBase, SettingsSvc,DataServer,User]

User.ts中的构造函数:

import {Injectable, Inject} from '@angular/core';
import {LocalDataBase} from './database';
import {Platform} from 'ionic-angular';
import {DataServer} from './dataServer';
import {Http, Response, Headers, RequestOptions} from '@angular/http';

@Injectable()
export class User {
constructor(private localDataBase: LocalDataBase,
    private platform: Platform,
    private dataServer: DataServer) {

DataServer中的构造函数:

import {Injectable} from '@angular/core';
import {Http, Response, Headers, RequestOptions} from '@angular/http';
import {AppSettings} from './appSettings';
import {Observable} from 'rxjs/Observable';
import '../import/rxjs-operators';

@Injectable()
export class DataServer {
constructor(private http: Http) {

有人有头绪吗?什么错了?

我已将@Inject添加到所有参数但未成功。

1 个答案:

答案 0 :(得分:0)

如果要将DataServer注入app.ts对象,该对象的子项将自动使用它。因此,您无需在User.ts

中注入它