computed中的get和set

GET 和 SET

  1. getter函数
负责使用data里面的数据,只能使用!无法修改,记得使用完之后return结果

data() {
    return {
        options: [{a: 1}]
    }
}
computed {
	// 写法一: 函数形式的简写:
	objArr() { return this.options;}
    
    // 写法二: 完整写法:
    objArr: {
        get() {
            return this.options;
        }
    }
}

2.setter函数

有一个参数 newValue, 负责接收传进来的值, 可以使用这个newValue给data中的数据进行操作

set(newValue) {
	// 可以在这里面对data数据进行操作, 这样get拿到的结果也会随data的值变化而变化
	this.data.options = newValue, // 用newValue替换原来的options
}
posted @ 2021-12-06 11:33  春天游泳。  阅读(508)  评论(0编辑  收藏  举报