我是面向對象編程的新手,我認爲這對於經驗豐富的面向對象程序員來說應該是一個簡單的概念,但我肯定會爲此而苦苦掙扎。如何將值從一個類的方法傳遞給Typescript中的另一個類的方法?
在我Angular2應用程序我有一個HTTPService類,如下圖所示:
http.service.ts
@Injectable()
export class HttpService {
constructor(private http: Http) { }
addLeaf(parentId, label, name){
var headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.post('http://localhost:8000/addleaf/',
{'parentId':parentId,'label':label, 'name':name},
{ headers: headers })
.map(res => res).subscribe();
}
我嘗試從如下另一個類中調用此方法:
leaf.ts
import { HttpService } from './http.service';
export class Leaf{
name: string;
...
http: Http; // very unsure about these two lines
private httpService: HttpService = new HttpService(this.http)
constructor(input){
this.name = input.name;
...
add(){
//what should go here?
this.httpservice.addLeaf(this.id, this.label, this.name);
//error -> cannot read property 'post' of undefined
}
閱讀this,我試着創建一個HttpService類的實例,但是我得到了post函數不存在的錯誤。也沒有運氣把httpService放在構造函數中。
我調用該方法在我的HTML這樣的:
(click)="leaf.add()"
編輯:以下@ peeskillet的回答我修改leaf.ts並添加leaf.component.ts如圖所示:
葉.TS
export class Leaf{
name: string;
...
constructor(input){
this.name = input.name;
...
add(){
//what should go here?
}
}
leaf.component.ts
@Component({
providers: [HttpService],
})
export class LeafComponent {
leaf: Leaf;
constructor(private httpService: HttpService) {
this.httpService.addLeaf(this.leaf.id, this.leaf.type, this.leaf.name)
}
}
服務工作正常,如果我在的地方則params的寫預定義的字符串,但還是不知道我怎麼能通過點擊的葉子的參數了這一點。
感謝您對服務的解釋,但我仍然不清楚如何將參數傳遞給該服務的方法。葉在我的情況下是一個對象,有幾個屬性(名稱,標籤等),我想將這些值傳遞給我的http服務。 – Arash
我不認爲該服務應該是葉的成員。我認爲你應該只有組件的葉和服務成員。然後,當您想發送請求時,只需從葉中獲取屬性並將其傳遞給服務方法即可。 –
是的,我從葉中刪除了服務並創建了一個新組件,在那裏添加了該服務,如果我在參數的位置放置了預定義的字符串,該服務可以正常工作,但我仍然不知道如何掛接我的當我點擊一個葉子對象到該服務時的參數。我將編輯該問題進行詳細說明。 – Arash