मैं Typescript
साथ angular2
उपयोग कर रहा हूँ के दौरान इंजेक्शन नहीं किया जा रहा। मैं एक base class
कि अन्य वर्गों द्वारा विरासत में मिला जा सकता है बनाने के लिए कोशिश कर रहा हूँ और आधार वर्ग के भीतर, एक सेवा इंजेक्ट किया जाता है। अब तक मुझे नहीं base class
कि user class
में inherited
किया जा रहा है में सही ढंग से ajaxService
injected
मिल सकती है। विशेष रूप से जब कोई उपयोगकर्ता तत्काल होता है, और save()
विधि को user
उदाहरण से कॉल किया जाता है, तो base class
में निम्न पंक्ति: return _this._ajaxService.send(options);
_ajaxService
के बाद से काम नहीं करता है।Angular2 निर्भरता इंजेक्शन, सेवा वर्ग विरासत
यहाँ एक user class
कि base 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
का उपयोग कैसे कर सकता हूं जब 'बेस मॉड्यूल किसी अन्य वर्ग पर बढ़ाया जा रहा है?
मुझे लगता है कि जब मैं उपयोगकर्ता का दृष्टांत, उपयोगकर्ता कक्षा में निर्माता कहा जाता है लेकिन आधार वर्ग कि सेवा injects में निर्माता नहीं बुलाया जा रहा है।
यहाँ 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);
});
});
}
}
जहां तक मुझे पता है कि इंजेक्शनबेल (अजाक्स सेवा) को टीएस में 'इंजेक्टेबल() 'के साथ एनोटेट किया जाना चाहिए। –
आप 'उपयोगकर्ता' कक्षा का उपयोग कहां करते हैं? क्या यह एक घटक है? क्या इसमें एक टेम्पलेट है? – yarons