Vue-监听属性

监听属性

简单监听

点击切换名字,来回变更Tom/Jerry,输出 你好,Tom/Jerry

  • 代码
javascript 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>监听属性</title>
    <!--  引入Vue  -->
    <script type="text/javascript" src="../js/vue.js"></script>
  </head>
  <body>
    <div id="root">
      <h1>监听属性</h1>
      <div>
        <h2>你好,{{name}}</h2>
        <button @click="changeName">切换名字</button>
      </div>
    </div>
  </body>
  <script type="text/javascript">
    Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示
    const vm = new Vue({
      el: "#root",
      data: {
        isTom: true
      },
      computed: {
        name(){
          return this.isTom ? "Tom" : "Jerry"
        }
      },
      methods: {
        changeName(){
          this.isTom = !this.isTom
        }
      },
      watch: {
        // isTom:{
        //   immediate:true, //初始化就执行handler方法
        //   // isTom发生改变时执行
        //   handler(newValue,oldValue){
        //     console.log("isTom切换了", oldValue + "->" + newValue)
        //   }
        // },
        name:{
          immediate:true, //初始化就执行handler方法
          // isTom发生改变时执行
          handler(newValue,oldValue){
            console.log("名字切换了", oldValue + "->" + newValue)
          }
        }
        
      },
    });
    vm.$watch('isTom',{
      immediate:true, //初始化就执行handler方法
      // isTom发生改变时执行
      handler(newValue,oldValue){
        console.log("isTom切换了", oldValue + "->" + newValue)
      }
    })
  </script>
</html>
  • 效果

深度监听

监听对象发生变更 numbers:{x:1,y:1}

对象某个属性监听 (x)

  • 代码
javascript 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>监听属性</title>
    <!--  引入Vue  -->
    <script type="text/javascript" src="../js/vue.js"></script>
  </head>
  <body>
    <div id="root">
      <h1>监听属性</h1>
      <div>
        <h2>你好,{{name}}</h2>
        <button @click="changeName">切换名字</button>
        <h2>单属性监听</h2>
        <button @click="numbers.x++">监听x+1</button>
      </div>
    </div>
  </body>
  <script type="text/javascript">
    Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示
    const vm = new Vue({
      el: "#root",
      data: {
        isTom: true,
        numbers:{
          x:1,
          y:2
        }
      },
      computed: {
        name(){
          return this.isTom ? "Tom" : "Jerry"
        }
      },
      methods: {
        changeName(){
          this.isTom = !this.isTom
        }
      },
      watch: {
        // isTom:{
        //   immediate:true, //初始化就执行handler方法
        //   // isTom发生改变时执行
        //   handler(newValue,oldValue){
        //     console.log("isTom切换了", oldValue + "->" + newValue)
        //   }
        // },
        name:{
          immediate:true, //初始化就执行handler方法
          // isTom发生改变时执行
          handler(newValue,oldValue){
            console.log("名字切换了", oldValue + "->" + newValue)
          }
        },
        'numbers.x':{
          immediate:true, //初始化就执行handler方法
          // isTom发生改变时执行
          handler(newValue,oldValue){
            console.log("x变更了", oldValue + "->" + newValue)
          }
        },
        numbers:{
          immediate:true, //初始化就执行handler方法
          // isTom发生改变时执行
          handler(newValue,oldValue){
            console.log("numbers变更了", oldValue + "->" + newValue)
          }
        }
        
      },
    });
    vm.$watch('isTom',{
      immediate:true, //初始化就执行handler方法
      // isTom发生改变时执行
      handler(newValue,oldValue){
        console.log("isTom切换了", oldValue + "->" + newValue)
      }
    })
  </script>
</html>
  • 效果

发现变更x, 并没有监听到numbers变更

属性变更能监听到整个对象的变化

深度监听 deep:true 可以监听对象内部的值改变(支持多层级)

  • 代码
javascript 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>监听属性</title>
    <!--  引入Vue  -->
    <script type="text/javascript" src="../js/vue.js"></script>
  </head>
  <body>
    <div id="root">
      <h1>监听属性</h1>
      <div>
        <h2>你好,{{name}}</h2>
        <button @click="changeName">切换名字</button>
        <h2>单属性监听</h2>
        <button @click="numbers.x++">监听x+1</button>
      </div>
    </div>
  </body>
  <script type="text/javascript">
    Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示
    const vm = new Vue({
      el: "#root",
      data: {
        isTom: true,
        numbers:{
          x:1,
          y:2
        }
      },
      computed: {
        name(){
          return this.isTom ? "Tom" : "Jerry"
        }
      },
      methods: {
        changeName(){
          this.isTom = !this.isTom
        }
      },
      watch: {
        // isTom:{
        //   immediate:true, //初始化就执行handler方法
        //   // isTom发生改变时执行
        //   handler(newValue,oldValue){
        //     console.log("isTom切换了", oldValue + "->" + newValue)
        //   }
        // },
        name:{
          immediate:true, //初始化就执行handler方法
          // isTom发生改变时执行
          handler(newValue,oldValue){
            console.log("名字切换了", oldValue + "->" + newValue)
          }
        },
        'numbers.x':{
          immediate:true, //初始化就执行handler方法
          // isTom发生改变时执行
          handler(newValue,oldValue){
            console.log("x变更了", oldValue + "->" + newValue)
          }
        },
        numbers:{
          immediate:true, //初始化就执行handler方法,
          deep:true,// 可以监听对象内部的值改变(支持多层级)
          // isTom发生改变时执行
          handler(newValue,oldValue){
            console.log("numbers变更了", JSON.stringify(oldValue) + "->" +  JSON.stringify(newValue))
          }
        }
        
      },
    });
    vm.$watch('isTom',{
      immediate:true, //初始化就执行handler方法
      // isTom发生改变时执行
      handler(newValue,oldValue){
        console.log("isTom切换了", oldValue + "->" + newValue)
      }
    })
  </script>
</html>
  • 效果

简写

前提条件:不需要特殊配置(deep、immediate)方可简写

  • 代码
javascript 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>监听属性</title>
    <!--  引入Vue  -->
    <script type="text/javascript" src="../js/vue.js"></script>
  </head>
  <body>
    <div id="root">
      <h1>监听属性</h1>
      <div>
        <h2>你好,{{name}}</h2>
        <button @click="changeName">切换名字</button>
      </div>
    </div>
  </body>
  <script type="text/javascript">
    Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示
    const vm = new Vue({
      el: "#root",
      data: {
        isTom: true,
      },
      computed: {
        name() {
          return this.isTom ? "Tom" : "Jerry";
        },
      },
      methods: {
        changeName() {
          this.isTom = !this.isTom;
        },
      },
      watch: {
        // 常规操作
        // name:{
        //   immediate:true, //初始化就执行handler方法
        //   // isTom发生改变时执行
        //   handler(newValue,oldValue){
        //     console.log("名字切换了", oldValue + "->" + newValue)
        //   }
        // },

        // 简写
        name(newValue, oldValue) {
          console.log("名字切换了", oldValue + "->" + newValue);
        },
      },
    });
    // 正常写法
    // vm.$watch('isTom',{
    //   immediate:true, //初始化就执行handler方法
    //   // isTom发生改变时执行
    //   handler(newValue,oldValue){
    //     console.log("isTom切换了", oldValue + "->" + newValue)
    //   }
    // })

    // 简写
    vm.$watch("isTom", function (newValue, oldValue) {
      console.log("isTom切换了", oldValue + "->" + newValue);
    });
  </script>
</html>
  • 效果
相关推荐
小贺要学前端14 小时前
【无标题】
前端·javascript·vue·技术趋势
IT教程资源C1 天前
(N_141)基于springboot,vue网上拍卖平台
mysql·vue·前后端分离·拍卖系统·springboot拍卖
IT教程资源C1 天前
(N_144)基于微信小程序在线订餐系统
mysql·vue·uniapp·前后端分离·订餐小程序·springboot订餐
合作小小程序员小小店1 天前
web网页开发,在线短视频管理系统,基于Idea,html,css,jQuery,java,springboot,mysql。
java·前端·spring boot·mysql·vue·intellij-idea
aiguangyuan2 天前
Vue 3.0 源码解读
vue·前端开发
IT教程资源D4 天前
[N_144]基于微信小程序在线订餐系统
mysql·vue·uniapp·前后端分离·订餐小程序·springboot订餐
是梦终空6 天前
vue下载依赖报错npm ERR node-sass@4.14.1 postinstall: `node scripts/build.js`的解决方法
javascript·npm·vue·node-sass·vue依赖
陈陈小白6 天前
npm run dev报错Error: listen EADDRINUSE: address already in use :::8090
前端·npm·node.js·vue
韩立学长6 天前
【开题答辩实录分享】以《证劵数据可视化分析项目设计与实现》为例进行答辩实录分享
python·信息可视化·vue
二当家的素材网7 天前
【无标题】
vue·uniapp