【发布时间】:2016-06-13 16:22:24
【问题描述】:
我希望我有合适的区域来要求这个...
下面是我正在处理的两个文件中的一些代码。当我将函数中带有“this.”的任何内容更改为箭头函数时,代码不再按预期运行。
有人能够解释我做错了什么吗?使用箭头函数时我需要做一些不同的事情吗?我在用“这个”吗?开始不正确?我不应该以这种方式使用箭头函数吗?
非常感谢您的帮助。
client/views/jobList.html
{{#each jobs}}
<tr>
<td>{{jobID}}</td>
<td>{{user.fullname}}</td>
<td>{{product.type}}</td>
<td>{{shortDesc}}</td>
<td>{{formattedDate}}</td>
<td>{{phone}}</td>
<td>{{user.username}}</td>
<td>{{jobType}}</td>
<td>{{product.brand}}</td>
<td>{{product.type}}</td>
<td>{{product.model}}</td>
</tr>
{{/each}}
client/views/jobList.js
Template.jobList.helpers({
jobs: ()=> Jobs.find({}, {sort: {jobDate: 1}}),
formattedDate: function() { // Changing this to arrow function breaks functionality
let d = this.jobDate;
let e = formatDate(d);
return e;
},
shortDesc: function () { // Changing this to arrow function breaks functionality
if (this.description.length > 40) {
return this.description.substr(0, 50) + '...';
} else {
return this.description
}
},
jobID: function () { // Changing this to arrow function breaks functionality
let a = this.jobNum;
let e = this.jobType.substr(0, 1);
return e + a
}
});
【问题讨论】:
-
请向我们展示您的箭头功能不起作用?我们告诉您它是否正确的最佳方式;)
-
如果我将帮助程序中包含
this的任何方法更改为箭头函数......在中编辑
标签: javascript meteor ecmascript-6 meteor-blaze