uni-app中使用computed计算属性

江枫思渺然 提交于 2020-08-17 20:20:21

computed的理解

computed里面的属性不能在data属性中出现,用来监控computed中自定义的变量
data() {
	return {
		url:"",
		mode:"SD",
		enableCamera:true,
		position:"front",
		beauty:0,
		whiteness:0,
		windowHeight:0,
		context:null,
		statusBarHeight:0,

		popupType:"mode"
	}
}
computed: {
	// 计算属性的 getter
	popupTitle() {
		let o = {
			mode: "画质",
			beauty: "美颜",
			whiteness: "美白"
		}
		return o[this.popupType]
	}
}
computed合适多个变量或对象处理后返回一个结果值,其中一个值发生变化则computed监控的属性值就会发生变化
<view class="flex align-center justify-center border-bottom" style="height: 90rpx;">
	<text class="font-md">{{popupTitle}}</text>
</view>
data() {
	return {
		url:"",
		mode:"SD",
		enableCamera:true,
		position:"front",
		beauty:0,
		whiteness:0,
		windowHeight:0,
		context:null,
		statusBarHeight:0,

		popupType:"mode"
	}
},
computed: {
	popupTitle() {
		let o = {
			mode: "画质",
			beauty: "美颜",
			whiteness: "美白"
		}
		return o[this.popupType]
	}
},
methods: {
	openPopup(type) {
		this.popupType = type
		this.$refs.popup.open()
	}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!