【发布时间】:2016-03-28 20:25:28
【问题描述】:
我是初学者,正在玩 Ionic2(写在 Angular2 上)。我需要帮助来解决我自己的服务中的依赖注入问题。
这是我的代码
import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http';
@Injectable()
export class Api {
constructor(public http: Http) {
console.log('api', http); // http is always undefined.
}
}
api = new Api();
其他情况,它有效。
import {Component, View} from 'angular2/angular2';
import {IONIC_DIRECTIVES} from 'ionic/ionic';
import {Http, Headers} from 'angular2/http';
import {Storage, LocalStorage} from 'ionic/ionic';
@Component({
selector: 'auth-login'
})
@View({
templateUrl: 'app/auth_login/auth_login.html',
directives: [IONIC_DIRECTIVES]
})
export class AuthLogin {
constructor(http: Http) {
console.log('login', http); // it works
}
}
谢谢大家。
【问题讨论】:
-
您能否更具体地说明您遇到的问题是什么?
-
抱歉,我知道要解决的信息较少,但我的代码非常简单。我添加了我编写的指令,它有效。
-
我找到了解决方案。我必须使用依赖注入,而不是构造函数。