为了减少重复的代码,我试图创建一个if语句来为变量分配一个函数或另一个函数,但这没有用!?
我试图做的是
const graphCall = (params['sfid'] === 'potential_engagements') ? this.engagementService.potentialsGraph() : this.engagementService.graph()
该语法本身不会引发错误,但是当我尝试使用时会
graphCall.then(animate =>
...不起作用! 我是否缺少某些东西,是否可以不分配功能,是否有另一种相似或不同的方式来检查和删除重复的代码?
我的代码:
if (params['sfid'] === 'potential_engagements') {
this.engagementService.potentialEngagements = true;
this.engagementService
.potentialsGraph()
.then(animate => {
this.graph = new EngagementGraph(d3.select('.Engagement-GraphSVG'));
this.track();
this.engagementService.isGraph = true;
this.engagementService
.getAllProducts()
.then(() => this.downloading = false)
.catch(err => this.pdfError = true)
this.findForumContact();
this.updateDateLabel();
this.addMemberPicture();
this.setup(animate);
})
.catch(error => this.handleError(error));
} else {
this.engagementService
.graph(params['sfid'])
.then(animate => {
this.graph = new EngagementGraph(d3.select('.Engagement-GraphSVG'));
this.track();
this.engagementService.isGraph = true;
this.engagementService
.getAllProducts()
.then(() => this.downloading = false)
.catch(err => this.pdfError = true)
this.findForumContact();
this.updateDateLabel();
this.addMemberPicture();
this.setup(animate);
})
.catch(error => this.handleError(error));
}
谢谢您的帮助!
答案 0 :(得分:1)
potential_engagements
块有两个其他块没有的东西:
this.engagementService.potentialEngagements = true; <------
this.engagementService
.potentialsGraph() <------
.then(animate => {
另一块只有另一块没有的东西:
this.engagementService
.graph(params['sfid']) <------
.then(animate => {
.then(animate
及其后的所有内容都是相同的,所以我建议将所有内容抽象为一个函数,也许称为handleGraphProm
:
const handleGraphProm = prom => prom.then(animate => {
this.graph = new EngagementGraph(d3.select('.Engagement-GraphSVG'));
this.track();
this.engagementService.isGraph = true;
this.engagementService
.getAllProducts()
.then(() => this.downloading = false)
.catch(err => this.pdfError = true)
this.findForumContact();
this.updateDateLabel();
this.addMemberPicture();
this.setup(animate);
})
.catch(error => this.handleError(error));
并调用:
if (params['sfid'] === 'potential_engagements') {
this.engagementService.potentialEngagements = true;
handleGraphProm(this.engagementService.potentialsGraph());
} else {
handleGraphProm(this.engagementService..graph(params['sfid']));
}
答案 1 :(得分:1)
它不起作用的原因可能是因为您还有一个附加的this.engagementService.potentialEngagements = true
,该附加值未在三元运算中分配。将thenable
和potentialsGraph
返回的graph
放入变量。然后在其上调用then
:
let graphCall;
if (params['sfid'] === 'potential_engagements') {
this.engagementService.potentialEngagements = true;
graphCall = this.engagementService.potentialsGraph()
} else {
graphCall = this.engagementService.graph(params['sfid'])
}
graphCall.then(animate => {
this.graph = new EngagementGraph(d3.select('.Engagement-GraphSVG'));
this.track();
this.engagementService.isGraph = true;
this.engagementService
.getAllProducts()
.then(() => this.downloading = false)
.catch(err => this.pdfError = true)
this.findForumContact();
this.updateDateLabel();
this.addMemberPicture();
this.setup(animate);
})
.catch(error => this.handleError(error));