Vue 'computed' 选项
实例
在 computed 选项中使用 computed 属性来显示相应的按钮文本。
export default {data() {return {msg: 'Hello World!',showMsg: false};},computed: {btnText() {if( this.showMsg ) {return 'Hide'}else {return 'Show'}}}};
定义与用法
computed 选项是一个具有 Vue 实例上声明的所有计算属性的对象。
计算属性通常是只读的(请参阅上面的实例),但也可以将计算属性定义为具有 get 和 set 函数的对象,这意味着计算属性也可以写入。
注意:在声明计算属性时应避免使用箭头函数,因为使用 this 关键字无法从此类函数内部访问 Vue 实例。
关联页面
Vue 教程: Vue Computed 属性
Vue 教程: Vue v-on 指令