【问题标题】:Angular 2 - Injectable Service need another Injectable Service - Uncaught Cannot resolve all parameters forAngular 2 - 可注入服务需要另一个可注入服务 - 未捕获无法解析所有参数
【发布时间】:2016-09-23 10:03:23
【问题描述】:

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

reflective_provider.js:232 Uncaught 无法解析所有参数 “用户”(LocalDataBase,平台,未定义)。确保所有 参数用 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 添加到所有参数但没有成功。

【问题讨论】:

    标签: javascript angularjs dependency-injection


    【解决方案1】:

    如果您将DataServer 注入app.ts 对象,它将自动可供该对象的子对象使用。因此,您不需要将其注入User.ts

    【讨论】:

    • 你的意思是我必须从 DataServer.ts 构造函数中删除“private dataServer: DataServer”吗?
    猜你喜欢
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 2014-07-22
    • 2016-08-07
    • 2017-08-24
    • 2017-11-06
    相关资源
    最近更新 更多