中に噴射されていません。私は他のクラスによって継承することができ、ベースクラス内では、サービスが注入されbase class
を作成しようとしています。これまでのところ私はuser class
へinherited
されbase class
に正しくajaxService
injected
を取得することはできません。ユーザがインスタンス化され、次いでsave()
方法はuser
インスタンスから呼び出されたとき、具体的に、base class
に次の行:_ajaxService
が不定であるためreturn _this._ajaxService.send(options);
は動作しません。ここでAngular2の依存性注入、サービスは、私が<code>Typescript</code>と<code>angular2</code>を使用しているクラスの継承
はbase class
を拡張することuser class
である:ここでは
import {Base} from '../utils/base';
export class User extends Base {
// properties
id = null;
email = null;
password = null;
first_name = null;
last_name = null;
constructor(source) {
_super.CopyProperties(source, this);
}
}
はbase class
です:
import {Component} from 'angular2/core';
import {AjaxService} from './ajax.service';
@Component({
providers: [AjaxService]
})
export class Base {
constructor(private _ajaxService: AjaxService) { }
// methods
public static CopyProperties(source:any, target:any):void {
for(var prop in source){
if(target[prop] !== undefined){
target[prop] = source[prop];
}
else {
console.error("Cannot set undefined property: " + prop);
}
}
}
save(options) {
const _this = this;
return Promise.resolve()
.then(() => {
const className = _this.constructor.name
.toLowerCase() + 's';
const options = {
data: JSON.stringify(_this),
url: className,
action: _this.id ? 'PATCH' : 'POST';
};
debugger;
return _this._ajaxService.send(options);
});
}
}
これはそのAjaxService
は、基本クラスに注入されていないことを除いて正常に動作します。私はユーザーがベースではなくインスタンス化されているので、これは理にかなっていると思います。
Base module
の中でAjaxService
を使用するにはどうすればベースモジュールを別のクラスで拡張するのですか?
私は、ユーザーをインスタンス化する場合、ユーザークラスのコンストラクタが呼び出されるが、サービスを注入基本クラスのコンストラクタが呼び出されていないと思います。
はここAjaxService
です:
import {Injectable} from 'angular2/core';
@Injectable()
export class AjaxService {
// methods
send(options) {
const endpoint = options.url || "";
const action = options.action || "GET";
const data = options.data || {};
return new Promise((resolve,reject) => {
debugger;
$.ajax({
url: 'http://localhost:3000' + endpoint,
headers: {
Authentication: "",
Accept: "application/vnd.app.v1",
"Content-Type": "application/json"
},
data: data,
method: action
})
.done((response) => {
debugger;
return resolve(response);
})
.fail((err) => {
debugger;
return reject(err);
});
});
}
}
注射液(AjaxService)にTSの '@Injectable()'と注釈を付ける必要があることは分かっています。 –
あなたは 'User'クラスをどこで使いますか?それはコンポーネントですか?それはテンプレートを持っていますか? – yarons