2017-03-02 153 views

回答

2

异议模型有virtualAttributes字段。从文档:

虚拟值不写入数据库。只有“外部”JSON格式将包含它们。

重要的是要注意这些是功能,而不仅仅是模型属性。从文档

实施例:

class Person extends Model { 
    static get virtualAttributes() { 
    return ['fullName', 'isFemale']; 
    } 

    fullName() { 
    return `${this.firstName} ${this.lastName}`; 
    } 

    get isFemale() { 
    return this.gender === 'female'; 
    } 
} 

const person = Person.fromJson({ 
    firstName: 'Jennifer', 
    lastName: 'Aniston', 
    gender: 'female' 
}); 

console.log(person.toJSON()); 
// --> {"firstName": "Jennifer", "lastName": "Aniston", "isFemale": true, "fullName": "Jennifer Aniston"} 
相关问题