![](/img/trans.png)
[英]How to call a function inside another function in Angular
[英]How to call custom service inside another services in angular 6
如何在角度6的另一個自定義服務中調用自定義服務
我創建了兩個服務user.services.ts和counter.services.ts
出現錯誤:無法調用類型缺少調用簽名的表達式。 類型“號碼”沒有兼容的呼叫簽名
甚至嘗試在app.modules providers數組中添加服務名稱,但出現相同錯誤
user.services.ts
import { Injectable } from '@angular/core';
import { CounterService } from './counter.service';
@Injectable({
providedIn: 'root',
})
export class UserService {
activeUsers = ['Max', 'Anna'];
inactiveUsers = ['Chris', 'Manu'];
constructor(private counterSer: CounterService) { }
setUserActive(id: number) {
this.activeUsers.push(this.inactiveUsers[id]);
this.inactiveUsers.splice(id, 1);
this.counterSer.activeToInactiveCounter();
}
setUserInactive(id: number) {
this.inactiveUsers.push(this.activeUsers[id]);
this.activeUsers.splice(id, 1);
}
}
Counter.services.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class CounterService {
constructor() { }
activeToInactiveCounter = 0;
inactiveToActiveCounter = 0;
incrementActiveToInactive() {
this.activeToInactiveCounter++;
console.log(this.activeToInactiveCounter);
}
incrementInactiveToActive() {
this.inactiveToActiveCounter++;
console.log(this.inactiveToActiveCounter);
}
}
您正在將屬性activeToInactiveCounter
稱為方法。 我認為您正在嘗試調用incrementActiveToInactive()
。
因此,而不是: this.counterSer.activeToInactiveCounter();
在setUserActive
,您應該具有: this.counterSer.incrementActiveToInactive();
setUserActive(id: number) {
this.activeUsers.push(this.inactiveUsers[id]);
this.inactiveUsers.splice(id, 1);
this.counterSer.incrementActiveToInactive();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.