【发布时间】: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