我正在学习Aurelia教程,但我只有意使用ES5和AMD/RequireJS,因为最初我试图减少我可能需要在当前的生产应用程序中引入的技术过载(该应用程序目前使用的是角,但我正在考虑将其转换到Aurelia,而不是角2),但我仍然坚持要让计算的属性正常工作。
我注意到,4月的最新情况从对象原型中删除了计算函数,这允许使用以下语法,但我不确定应该做什么,而不是使用以下语法:
Welcome.computed({
fullName : function() { return this.firstName + " " + this.lastName; }
});我可以实现与以下相同的效果,但它似乎相当冗长,考虑到什么是正在实现的!有正确的方法吗?
Object.defineProperty(
Welcome.prototype,
"fullName",
{
get: function() { return this.firstName + " " + this.lastName },
enumerable: true
});发布于 2015-08-15 23:40:04
您所做的是正确的-您可以总是重新添加计算工厂方法,因为编写所有这些额外的ES5代码可能会变得乏味。
我还建议指定计算属性的依赖项以优化数据绑定效率。以下代码片段是等效的:
ES5:
function fullName() { return this.firstName + ' ' + this.lastName }
fullName.dependencies = ['firstName', 'lastName'];
Object.defineProperty(
Welcome.prototype,
'fullName',
{
get: fullName,
enumerable: true
});ES-10/ES-10:
@computedFrom('firstName', 'lastName')
get fullName() {
return `${this.firstName} ${this.lastName}`;
}http://aurelia.io/docs.html#adaptive-binding
https://stackoverflow.com/questions/32028553
复制相似问题