angular - 通过服务设置值后偶尔会得到未定义
问题描述
我有公共领域在服务:
@Injectable()
export class ShareDataService {
// some code
public templateForCurrencyCOS;
public templateForPercentCOS;
public templateForCurrencyCOGS;
public templateForPercentCOGS;
// some code
}
事实上,这个字段的值是HeaderComponent
通过订阅 other 来设置的revenueService
:
export class HeaderComponent implements OnInit, OnDestroy {
constructor(
private revenueService: RevenueService
) {}
ngOnInit() {
this.addCOSItemCurrencyTemplate();
this.addCOSItemPercentTemplate();
this.addCOGSItemCurrencyTemplate();
this.addCOGSItemPercentTemplate();
}
addCOSItemCurrencyTemplate() {
if (!this.shareDataService.templateForCurrencyCOS) {
this.revenueService.addCostOfSellingItemCurrencyNew().subscribe(
response => {
this.shareDataService.templateForCurrencyCOS = response;
}, error => {
console.log(error);
});
}
}
addCOSItemPercentTemplate() {
if (!this.shareDataService.templateForPercentCOS) {
this.revenueService.addCOSPercentNew().subscribe(response =>{
this.shareDataService.templateForPercentCOS = response;
}, error => {
console.log(error);
});
}
addCOGSItemCurrencyTemplate() {
// similar implementation like addCOSItemPercentTemplate and addCOSItemCurrencyTemplate
}
addCOGSItemPercentTemplate() {
// similar implementation like addCOSItemPercentTemplate and addCOSItemCurrencyTemplate
}
}
}
在这里,我不时得到undefined
for
this.shareDataService.templateForCurrencyCOGS
以及 for
this.shareDataService.templateForCurrencyCOS
,
this.shareDataService.templateForPercentCOGS
,
this.shareDataService.templateForPercentCOS
export class RevenueAssistantComponent implements OnInit {
ngOnInit() {
this.templateForCurrencyCOGS = {...
this.shareDataService.templateForCurrencyCOGS,
fields: [...this.shareDataService.templateForCurrencyCOGS.fields]
};
this.templateForPercentCOGS = {...
this.shareDataService.templateForPercentCOGS,
fields: [...this.shareDataService.templateForPercentCOGS && this.shareDataService.templateForPercentCOGS.fields]
};
this.templateForCurrencyCOS = {
...this.shareDataService.templateForCurrencyCOS,
fields: [...this.shareDataService.templateForCurrencyCOS && this.shareDataService.templateForCurrencyCOS.fields]
};
this.templateForPercentCOS = {
...this.shareDataService.templateForPercentCOS,
fields: [...this.shareDataService.templateForPercentCOS &&
this.shareDataService.templateForPercentCOS.fields]
};
}
}
我猜这个问题背后的原因是在为字段设置值RevenueAssistantComponent
之前 initsHeaderComponent
和解决方法是
在字段的设置值 之后始终RevenueAssistantComponent
获取模板值。但我不知道如何实现它HeaderComponent
shareDataService
解决方案
在声明时初始化将解决错误。
public templateForCurrencyCOS = {}; or templateForCurrencyCOS = []; // for Arrays
public templateForPercentCOS = {};
public templateForCurrencyCOGS = {};
public templateForPercentCOGS = {};
推荐阅读
- reactjs - 为什么我无法在 React Hooks 中只使用一次道具?
- discord.py - 更改用户昵称时出现禁止错误
- apache-kafka - Wildfly 上的 Kafka 客户端抛出不受支持的回调异常
- python - tkinter 主类 App(tk.Tk) 使用哪些构造函数参数?
- java - 如何使用 ProcessBuilder 重定向 python 脚本的输出?
- javascript - TypeError:axiosCookieJarSupport 不是函数
- c++ - 错误“将 const 字符串绑定到类型 x 的引用”是什么意思?
- selenium - 如何为在 Kubernetes 中运行的 Selenium 节点打开浏览器会话
- server - 如何在不更改路由器端口的情况下从世界任何地方访问我的本地主机?
- javascript - 如何将一个简单的 HTML/CSS/JS 页面部署到 heroku、netlify