Vue-监听属性

news2025/5/20 12:16:30

监听属性

简单监听

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

在这里插入图片描述

  • 代码
<!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)

  • 代码
<!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 可以监听对象内部的值改变(支持多层级)

  • 代码
<!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)方可简写

  • 代码
<!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>

  • 效果

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2379954.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

python fastapi + react, 写一个图片 app

1. 起因&#xff0c; 目的: 上厕所的时候&#xff0c;想用手机查看电脑上的图片&#xff0c;但是又不想点击下载。此app 应运而生。 2. 先看效果 单击图片&#xff0c;能放大图片 3. 过程: 过程很枯燥。有时候&#xff0c; 有一堆新的想法。 但是做起来太麻烦&#xff0c;…

vscode c++编译onnxruntime cuda 出现的问题

问题描述 将onnx的dll文件和lib文件copy到可执行文件所在文件夹下后&#xff0c;现象&#xff1a; 双击可执行文件能正常运行 在vscode中点击cmake插件的运行按钮出现报错为 c [ONNXRuntimeError] : 1 : FAIL : LoadLibrary failed with error 126 “” when trying to load尝试…

中服云生产线自动化智能化调度生产系统:打造智能制造新标杆

前言 在当今制造业竞争日益激烈的背景下&#xff0c;实现生产线的自动化与智能化已成为企业提升竞争力的关键。作为国内技术领先的工业物联网平台、数字孪生、自动控制技术厂商&#xff0c;中服云凭借其深厚的技术积累和创新能力&#xff0c;打造了一套完整的生产线自动化智能…

云鼎入鼎系统:一站式电商管理解决方案

个人名片 &#x1f393;作者简介&#xff1a;java领域优质创作者 &#x1f310;个人主页&#xff1a;码农阿豪 &#x1f4de;工作室&#xff1a;新空间代码工作室&#xff08;提供各种软件服务) &#x1f48c;个人邮箱&#xff1a;[2435024119qq.com] &#x1f4f1;个人微信&a…

Leetcode134加油站

题目链接 134 题意图解&#xff1a; 题目给了n个节点&#xff0c;这些节点呈现环状&#xff0c;每次到一个低点要消耗cost[i]的油量。 从中我们可以得出一个结论&#xff1a;看一个点能不能到下一个点&#xff0c;就要用当前的油量减去消耗的量&#xff0c;那么gas[i] - cost…

关于Android Studio for Platform的使用记录

文章目录 简单介绍如何使用配置导入aosp工程配置文件asfp-config.json 简单介绍 Android Studio for Platform是google最新开发&#xff0c;用来阅读aosp源码的工具 详细的资料介绍&#xff1a; https://developer.android.google.cn/studio/platform 将工具下载下来直接点击…

uniapp 微信小程序 获取openId

嗨&#xff0c;我是小路。今天主要和大家分享的主题是“uniapp 微信小程序 获取openId”。 一、主要属性 1.uni.login 二、实例代码 1、前端代码 uni.login({provider: weixin,success: (res) > {uni.showLoading({title: 登录中...,mask: true})let code res.…

隧道结构安全在线监测系统解决方案

一、方案背景 隧道是地下隐蔽工程&#xff0c;会受到潜在、无法预知的地质因素影响。随着我国公路交通建设的发展&#xff0c;隧道占新建公路里程的比例越来越大。隧道属于线状工程&#xff0c;有的规模较大&#xff0c;可长达几公里或数十公里&#xff0c;往往穿越许多不同环境…

Docker 运维管理

Docker 运维管理 一、Swarm集群管理1.1 Swarm的核心概念1.1.1 集群1.1.2 节点1.1.3 服务和任务1.1.4 负载均衡 1.2 Swarm安装准备工作创建集群添加工作节点到集群发布服务到集群扩展一个或多个服务从集群中删除服务ssh免密登录 二、Docker Compose与 Swarm 一起使用 Compose 三…

[SpringBoot]Spring MVC(2.0)

紧接上文&#xff0c;这篇我们继续讲剩下的HTTp请求 传递JSON数据 简单来说&#xff1a;JSON就是⼀种数据格式,有⾃⼰的格式和语法,使⽤⽂本表⽰⼀个对象或数组的信息,因此JSON本质是字符串. 主要负责在不同的语⾔中数据传递和交换 JSON的语法 1. 数据在 键值对(Key/Value) …

Golang的网络安全策略实践

Golang的网络安全策略实践 一、理解网络安全的重要性 当今的网络环境中&#xff0c;安全问题日益突出&#xff0c;各种类型的攻击如雨后春笋般涌现&#xff0c;给个人和组织的信息资产造成了严重威胁。因此&#xff0c;制定和实施有效的网络安全策略至关重要。 二、Golang在网络…

STM32外设AD-轮询法读取模板

STM32外设AD-轮询法读取模板 一&#xff0c;什么是轮询&#xff1f;1&#xff0c;轮询法的直观理解2&#xff0c;轮询法缺点 二&#xff0c;CubeMX配置三&#xff0c;模板移植1&#xff0c;adc_app.c文件2&#xff0c;变量声明1&#xff0c;adc_app.c中2&#xff0c;mydefine.h…

iOS音视频解封装分析

首先是进行解封装的简单的配置 /// 解封装配置 class KFDemuxerConfig {// 媒体资源var asset: AVAsset?// 解封装类型&#xff0c;指定是音频、视频或两者都需要var demuxerType: KFMediaType .avinit() {} }然后是实现解封装控制器 import Foundation import CoreMedia i…

突破智能驾舱边界,Imagination如何构建高安全GPU+AI融合计算架构

日前&#xff0c;“第十二届汽车电子创新大会暨汽车芯片产业生态发展论坛&#xff08;AEIF 2025&#xff09;”在上海顺利举办。大会围绕汽车前沿性、关键性和颠覆性技术突破&#xff0c;邀请行业众多专家学者&#xff0c;分享与探讨了汽车电子产业的技术热点与发展趋势。在5月…

DeepSeek 如何实现 128K 上下文窗口?

DeepSeek 如何实现 128K 上下文窗口&#xff1f;长文本处理技术揭秘 系统化学习人工智能网站&#xff08;收藏&#xff09;&#xff1a;https://www.captainbed.cn/flu 文章目录 DeepSeek 如何实现 128K 上下文窗口&#xff1f;长文本处理技术揭秘摘要引言技术架构解析1. 动态…

Python 实现图片浏览和选择工具

实现将截图预览&#xff0c;并按照顺序加入一个pdf文件中&#xff0c;实现照片管理尤其对于喜欢看教程截图做笔记的网友们。 C:\pythoncode\new\python-image-pdf-processor.py 界面展示 &#x1f9f1; 一、核心结构概述 主类 ImageViewer(wx.Frame) 是主窗口类&#xff0c;…

Python实现的在线词典学习工具

Python实现的在线词典学习工具 源码最初来自网络&#xff0c;根据实际情况进行了修改。 主要功能&#xff1a; 单词查询 通过Bing词典在线获取单词释义&#xff08;正则提取网页meta描述&#xff09;&#xff0c;支持回车键快速查询 内置网络请求重试和异常处理机制 在线网页…

BGP综合实验(2)

一、实验需求 1、实验拓扑图 2、实验需求 使用 PreVal 策略&#xff0c;让 R4 经 R2 到达 192.168.10.0/24 。 使用 AS_Path 策略&#xff0c;让 R4 经 R3 到达 192.168.11.0/24 。 配置 MED 策略&#xff0c;让 R4 经 R3 到达 192.168.12.0/24 。 使用 Local Preference 策…

代码随想录算法训练营 Day51 图论Ⅱ岛屿问题Ⅰ

图论 题目 99. 岛屿数量 使用 DFS 实现方法 判断岛屿方法 1. 遍历图&#xff0c;若遍历到了陆地 grid[i][j] 1 并且陆地没有被访问&#xff0c;在这个陆地的基础上进行 DFS 方法&#xff0c;或者是 BFS 方法 2. 对陆地进行 DFS 的时候时刻注意以访问的元素添加访问标记 //…

【占融数科-注册/登录安全分析报告】

前言 由于网站注册入口容易被黑客攻击&#xff0c;存在如下安全问题&#xff1a; 暴力破解密码&#xff0c;造成用户信息泄露短信盗刷的安全问题&#xff0c;影响业务及导致用户投诉带来经济损失&#xff0c;尤其是后付费客户&#xff0c;风险巨大&#xff0c;造成亏损无底洞…