vue conputed使用方法

21 篇文章 0 订阅

1、普通用法

<div id="example">
  <p>Original message: "{{ message }}"</p>
  <p>Computed reversed message: "{{ reversedMessage }}"</p>
</div>


var vm = new Vue({
  el: '#example',
  data: {
    message: 'Hello'
  },
  computed: {
    // 计算属性的 getter
    reversedMessage: function () {
      // `this` 指向 vm 实例
      return this.message.split('').reverse().join('')
    }
  }
})

2、带参数的计算

<el-table-column label="房间号" >
            <template slot-scope="scope">
              <span :title="errorShow('房间号',scope.row.error.list)" :class="errorWarn('房间号',scope.row.error.list)"> {{ scope.row['房间号'] |Empty}}</span>
            </template>
          </el-table-column>


computed: {
      errorShow() {
        return function(title,list) {
          for (let item of list) {
            if(item.key==title){
              return item.Tip
            }
          }
        }
      },
      errorWarn() {
        return function(title,list) {
          for (let items of list) {
            if(items.key==title&&items.Level=='Error'){
              return 'error'
            }
            else if(items.key==title&&items.Level=='Warn'){
              return 'warn'
            }
            else {

            }
          }
        }
      }
    },

3、get、settter

<template>
  <div id="example">
    <input v-model="helloworld"/>
    <p>{{ hello }}</p>
    <p>{{ world }}</p>
    <p>{{ helloworld }}</p>
    <button @click="changedata">事件触发</button>
  </div>
</template>
<script>
export default {
  data () {
    return {
      hello: 'hello',
      world: 'world'
    }
  },
  computed: { // getter,setter计算属性都有
    helloworld: {
      // getter
      get: function () {
        console.log('getter called')
        return this.hello + this.world
      },
      // setter
      set: function (newValue) {
        console.log('setter called , newValue: ' + newValue)
        this.hello = newValue
      }
    }
  },
  methods: {
    changedata () {
      // this.hello = 'hello~' //并不能触发set
      this.helloworld = 'hello~world!'
    }
  }
}
</script>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值