vue-cli: 计算属性与侦听属性

一. 计算属性与侦听属性

<template>
  <div>
    <h1>计算属性和侦听属性</h1>
    <!-- 表达式太复杂的话会导致模板难以维护, 而且如果多个地方使用该表达式的话会导致重复, -->
    <div>
      {{ type + ':' + msg }}
    </div>
    <!-- 使用computed计算属性 -->
    <div>{{ type_msg }}</div>
    <div>{{ firstName }}</div>
    <div>{{ lastName }}</div>
    <div>{{ fullName }}</div>
    <div>{{ fullName }}</div>
    <button @click="modifyFirstName">修改firstName</button>
    <button @click="modifyLastName">修改lastName</button>
    <button @click="modifyFullName">修改fullName</button>
    <!-- 通过函数方式达到计算属性的效果, 不推荐使用: 因为DOM更新的时候computed属性可以缓存, 而函数会反复调用, 导致性能低  -->
    <div>{{ getFullName() }}</div>
    <!-- computed使用场景: 某个数据受多个数据影响, 或者需要对其他数据进行js处理再显示;
     watch使用场景: 一个数据的变化会影响多个数据, 或者一个数据变化的时候需要执行异步操作 -->
    <div>语言: {{ language }}</div>
    <button @click="switch1('ch')">中文</button>
    <button @click="switch1('en')">English</button>
    <div>{{ ZhangSan }}</div>
    <div>{{ LiSi }}</div>
  </div>
</template>

<script>
export default {
  name: 'Demo7',
  data () {
    return {
      a: '11',
      msg: 'demo1',
      type: 'news',
      firstName: '孙',
      lastName: '悟空',
      // fullName: '' // 此处是用watch的时候需要的变量
      language: 'ch',
      // ZhangSan: '张三',
      // LiSi: '李四'
      ZhangSan: '',
      LiSi: ''
    }
  },
  computed: {
    type_msg () {
      return this.type + ':' + this.msg
    },
    // fullName () {
    //   return this.firstName + '' + this.lastName
    // },
    fullName: {
      get () {
        return this.firstName + '' + this.lastName
      },
      set (newVal) {
        const arr = newVal.split(' ')
        this.firstName = arr[0]
        this.lastName = arr[1]
      }
    }
  },
  methods: {
    getFullName () {
      return this.firstName + '' + this.lastName
    },
    modifyFirstName () {
      this.firstName = '上官'
    },
    modifyLastName () {
      this.lastName = '翠花'
    },
    modifyFullName () {
      this.fullName = 'Michel Jordan'
    },
    switch1 (val) {
      this.language = val
    }
  },
  watch: {
    // language (newVal) {
    //   if (newVal === 'ch') {
    //     this.ZhangSan = '张三'
    //     this.LiSi = '李四'
    //   } else if (newVal === 'en') {
    //     this.ZhangSan = 'ZhangSan'
    //     this.LiSi = 'LiSi'
    //   }
    // }
    language: {
      handler (newVal) {
        if (newVal === 'ch') {
          this.ZhangSan = '张三'
          this.LiSi = '李四'
        } else if (newVal === 'en') {
          this.ZhangSan = 'ZhangSan'
          this.LiSi = 'LiSi'
        }
      },
      // 立即执行
      immediate: true
    }
  }
  // 此处场景不适用, 没有computed简单好用
  // watch: {
  //   firstName (newVal) {
  //     this.fullName = newVal + '' + this.lastName
  //   },
  //   lastName (newVal) {
  //     this.fullName = this.lastName + '' + newVal
  //   }
  // }
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值