export default {
props: {
endPoint: {
type: Object
}
},
computed: {
endPointWidth: {
get() {
return this.endPoint.width;
},
set(newWidth) {
this.$emit('update:endPoint', { ...this.endPoint, width: newWidth });
}
},
endPointHeight: {
get() {
return this.endPoint.height;
},
set(newHeight) {
this.$emit('update:endPoint', { ...this.endPoint, height: newHeight });
}
}
}
}
属性多的话:
export default {
props: {
endPoint: {
type: Object
}
},
computed: {
...generateComputedProperties(['width', 'height', 'depth', 'color'])
}
}
function generateComputedProperties(props) {
let computed = {};
props.forEach(prop => {
computed[`endPoint${capitalizeFirstLetter(prop)}`] = {
get() {
return this.endPoint[prop];
},
set(value) {
this.$set(this.endPoint, prop, value);
}
};
});
return computed;
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}