Vue的computed和watch

computed(计算属性)

用途

被计算出来的属性就是计算属性

代码示例

// 引用完整版 Vue,方便讲解
import Vue from "vue/dist/vue.js";
Vue.config.productionTip = false;
new Vue({
  data: {
    user: {
      email: "122322323@qq.com",
      nickName: "jack",
      phone: "13812312312"
    }
  },
  computed: {
    displayName: {
      get() {
        const user = this.user;
        return user.nickname || user.email || user.phone;
      },
      set(value) {
        console.log(value);
        this.user.nickname = value;
      }
    }
  },
  // DRY don't repeat yourself
  // 不如用 computed 来计算 displayName
  template: `
    <div>
      {{displayName}}
      <div>
      {{displayName}}
      <button @click="add">set</button>
      </div>
    </div>
  `,
  methods: {
    add() {
      console.log("add");
      this.displayName = "圆圆";
    }
  }
}).$mount("#app");

缓存

  • 如果依赖的缓存没有变化,就不会重新计算
  • getter/setter默认不做缓存,Vue做了特殊处理

watch(监听)

用途

  • 当数据发送变化时,执行一个函数

代码示例

import Vue from "vue/dist/vue.js";
Vue.config.productionTip = false;

new Vue({
  data: {
    n: 0,
    history: [],
    inUndoMode: false
  },
  watch: {
    n: function (newV, oldV) {
      console.log(this.inUndoMode);
      if (!this.inUndoMode) {
        this.history.push({ from: oldV, to: newV });
      }
    }
  },
  // 不如用 computed 来计算 displayName
  template: `
        <div>
            {{n}}
            <hr />
            <button @click="add1">+1</button>
            <button @click="add2">+2</button>
            <button @click="minus1">-1</button>
            <button @click="minus2">-2</button>
            <hr/>
            <button @click="undo">撤销</button>
            <hr/>

            {{history}}
        </div>
    `,
  methods: {
    add1() {
      this.n += 1;
    },
    add2() {
      this.n += 2;
    },
    minus1() {
      this.n -= 1;
    },
    minus2() {
      this.n -= 2;
    },
    undo() {
      const last = this.history.pop();
      this.inUndoMode = true;
      console.log("ha" + this.inUndoMode);
      const old = last.from;
      this.n = old; // watch n 的函数会异步调用
      this.$nextTick(() => {
        this.inUndoMode = false;
      });
    }
  }
});

immediate和handler的用法

这样使用watch时有一个特点,就是当值第一次绑定的时候,不会执行监听函数,只有值发生改变才会执行。如果我们需要在最初绑定值的时候也执行函数,则就需要用到immediate属性

new Vue({
  el: '#root',
  data: {
    cityName: ''
  },
  watch: {
    cityName: {
      handler(newName, oldName) {
        // ...      },
      immediate: true
    }
  } 
})

deep

  • 可以理解为深拷贝
  • 如果只是监听对象的话,deep就设置为false
  • 如果要监听对象里面的属性变化,deep就设置为true
new Vue({
  el: '#root',
  data: {
    cityName: {id: 1, name: 'shanghai'}
  },
  watch: {
    cityName: {
      handler(newName, oldName) {      // ...    },
    deep: true,
    immediate: true
    }
  } 
})
已标记关键词 清除标记
相关推荐
<p style="font-size:16px;"> <img src="https://img-bss.csdnimg.cn/202005310007118712.png" alt="" /> </p> <p style="font-size:16px;"> <br /> </p> <p style="font-size:16px;"> <br /> </p> <p style="font-size:16px;"> <span style="color:#FFFF00;background-color:#FF0000;font-size:24px;"><strong>走在技术前沿之 Vue3.0 新特性全面解析。 <strong>全网抢先看,做首批 Vue 3.0 开发者!!!</strong></strong></span> </p> <p style="font-size:16px;"> <br /> </p> <p style="font-size:16px;"> <br /> </p> <p style="font-size:16px;"> <span style="font-size:20px;color:#595959;"><strong>课程内容包含:<br /> </strong></span> </p> <p style="font-size:16px;"> <span style="font-size:20px;color:#595959;"><strong>1.Vue 3.0之版本大更新</strong></span> </p> <p style="font-size:16px;"> <span style="font-size:20px;color:#595959;"><strong>2.新特性重点关注</strong></span> </p> <p style="font-size:16px;"> <span style="font-size:20px;color:#595959;"><strong>3.composition-api(组合式API)</strong></span> </p> <p style="font-size:16px;"> <span style="font-size:20px;color:#595959;"><strong>4.初始化Vue 3.0项目</strong></span> </p> <p style="font-size:16px;"> <span style="font-size:20px;color:#595959;"><strong>5.setup函数</strong></span> </p> <p style="font-size:16px;"> <span style="font-size:20px;color:#595959;"><strong>6.响应式系统API(reactive、ref、computedwatch等)</strong></span> </p> <p style="font-size:16px;"> <span style="font-size:20px;color:#595959;"><strong>7.响应式系统工具集(unref、toRef、toRefs、isRef、isProxy等)<br /> </strong></span> </p> <p style="font-size:16px;"> <span style="font-size:20px;color:#595959;"><strong>8.生命周期钩子函数的变化</strong></span> </p> <p style="font-size:16px;"> <span style="font-size:20px;color:#595959;"><strong>9.依赖注入</strong></span> </p> <p style="font-size:16px;"> <span style="font-size:20px;color:#595959;"><strong>10.模板refs</strong></span> </p> <p style="font-size:16px;"> <br /> </p> <p style="font-size:16px;"> <strong><span style="color:#FF0000;">教学全程采用笔记+代码案例的形式讲解,通俗易懂!!!</span></strong> </p> <p style="font-size:16px;"> <strong><span style="color:#FF0000;"><br /> </span></strong> </p> <p style="font-size:16px;"> <img src="https://img-bss.csdnimg.cn/202005310011021534.png" alt="" /> </p> <p style="font-size:16px;"> <br /> </p> <p style="font-size:16px;"> <img src="https://img-bss.csdnimg.cn/202005310011101181.png" alt="" /> </p> <p style="font-size:16px;"> <strong><span style="color:#FF0000;"><br /> </span></strong> </p> <p style="font-size:16px;"> <img src="https://img-bss.csdnimg.cn/202005310011183568.png" alt="" /> </p> <p style="font-size:16px;"> <strong><span style="color:#FF0000;"><br /> </span></strong> </p> <p style="font-size:16px;"> <img src="https://img-bss.csdnimg.cn/202005310011275518.png" alt="" /> </p> <p style="font-size:16px;"> <strong><span style="color:#FF0000;"><br /> </span></strong> </p> <p style="font-size:16px;"> <strong><span style="color:#FF0000;"><br /> </span></strong> </p> <p style="font-size:16px;"> <span style="color:#C00000;"><strong><span><strong>补充:想学习 Vue 2.x 的同学请移步以下地址</strong></span></strong><strong><span><strong></strong></span></strong></span> </p> <p style="font-size:16px;"> <span style="color:#C00000;"><strong><span style="color:#FF0000;"><strong><a href="https://edu.51cto.com/course/10543.html"></a><a href="https://edu.csdn.net/course/detail/7906">https://edu.csdn.net/course/detail/7906</a> (</strong></span></strong><span style="color:#FF0000;"><strong><span></span></strong><strong><span></span></strong><strong><span><strong>Vue.js 2.0之全家桶系列视频课程)</strong></span></strong></span></span> </p> <p style="font-size:16px;"> <span style="color:#FF0000;"><strong><span><strong><span style="font-size:18px;"> </span></strong></span></strong></span> </p> <p style="font-size:16px;"> <strong><span style="color:#FF0000;"><br /> </span></strong> </p> <p style="font-size:16px;"> <strong><span style="color:#FF0000;"></span></strong> </p> <p style="font-size:16px;"> <span style="font-size:18px;"><strong>讲师介绍</strong></span> </p> <p style="font-size:16px;"> <img src="https://img-bss.csdnimg.cn/202005310012193677.jpg" alt="" /> </p> <p style="font-size:16px;"> <strong><span style="color:#FF0000;"><br /> </span></strong> </p>
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页