当前位置: 首页 > news >正文

公司品牌网站建设价格网站seo顾问

公司品牌网站建设价格,网站seo顾问,乌当区城乡建设局网站,中国城乡住房和城乡建设部网站在 Vue 2 和 Vue 3 中,watch 用于监听数据的变化并执行相应的逻辑。虽然两者的核心功能相同,但在语法和使用方式上有一些区别。以下是 Vue 2 和 Vue 3 中使用 watch 的详细说明: Vue 2 中的 watch 在 Vue 2 中,watch 是通过选项式…

在 Vue 2 和 Vue 3 中,watch 用于监听数据的变化并执行相应的逻辑。虽然两者的核心功能相同,但在语法和使用方式上有一些区别。以下是 Vue 2 和 Vue 3 中使用 watch 的详细说明:

Vue 2 中的 watch

在 Vue 2 中,watch 是通过选项式 API 实现的,通常在组件的 watch 选项中定义。

【基本用法】

export default {data() {return {message: 'Hello Vue 2',count: 0,};},watch: {// 监听 message 的变化message(newVal, oldVal) {console.log('message changed:', newVal, oldVal);},// 监听 count 的变化count(newVal, oldVal) {console.log('count changed:', newVal, oldVal);},},
};

【监听对象属性】
如果需要监听对象的某个属性,可以使用字符串形式的键名:

export default {data() {return {user: {name: 'Alice',age: 25,},};},watch: {'user.name'(newVal, oldVal) {console.log('user.name changed:', newVal, oldVal);},},
};

【深度监听】
如果需要监听对象或数组内部的变化,可以设置 deep: true:

export default {data() {return {user: {name: 'Alice',age: 25,},};},watch: {user: {handler(newVal, oldVal) {console.log('user changed:', newVal, oldVal);},deep: true, // 深度监听},},
};

【立即执行】
如果需要监听器在创建时立即执行一次,可以设置 immediate: true:

export default {data() {return {message: 'Hello Vue 2',};},watch: {message: {handler(newVal, oldVal) {console.log('message changed:', newVal, oldVal);},immediate: true, // 立即执行},},
};

Vue3中的watch

在 Vue 3 中,watch 是通过 Composition API 实现的,使用 watch 函数来定义监听器。
【基本用法】

import { ref, watch } from 'vue';export default {setup() {const message = ref('Hello Vue 3');const count = ref(0);// 监听 message 的变化watch(message, (newVal, oldVal) => {console.log('message changed:', newVal, oldVal);});// 监听 count 的变化watch(count, (newVal, oldVal) => {console.log('count changed:', newVal, oldVal);});return {message,count,};},
};

【监听多个数据】

import { ref, watch } from 'vue';export default {setup() {const firstName = ref('Alice');const lastName = ref('Smith');// 监听 firstName 和 lastName 的变化watch([firstName, lastName], ([newFirstName, newLastName], [oldFirstName, oldLastName]) => {console.log('firstName or lastName changed:', newFirstName, newLastName);});return {firstName,lastName,};},
};

【监听对象属性】

import { ref, watch } from 'vue';export default {setup() {const user = ref({name: 'Alice',age: 25,});// 监听 user.name 的变化watch(() => user.value.name,(newVal, oldVal) => {console.log('user.name changed:', newVal, oldVal);});return {user,};},
};

【深度监听】
在 Vue 3 中,默认情况下 watch 是浅层的。如果需要深度监听,可以设置 { deep: true }:

import { ref, watch } from 'vue';export default {setup() {const user = ref({name: 'Alice',age: 25,});// 深度监听 user 对象watch(user,(newVal, oldVal) => {console.log('user changed:', newVal, oldVal);},{ deep: true });return {user,};},
};

【立即执行】
如果需要监听器在创建时立即执行一次,可以设置 { immediate: true }:

import { ref, watch } from 'vue';export default {setup() {const message = ref('Hello Vue 3');// 立即执行监听器watch(message,(newVal, oldVal) => {console.log('message changed:', newVal, oldVal);},{ immediate: true });return {message,};},
};
http://www.yayakq.cn/news/822585/

相关文章:

  • 网站怎么做优化排名手机app开发工资高吗
  • 济南做外贸网站电子工程网官方网站
  • 制作一个网站的步骤怎样做校园网站推广
  • 网站备案成功下载应用商店app
  • 门户网站开发 南宁科技官网
  • 网站设计远程培训号卡分销系统源码
  • 免费做网站的app做网站需要报备什么条件
  • 怎么将公司网站设成首页查域名138
  • 做一个网站需要多长时间熊掌号接入wordpress
  • 淘宝客网站如何让做网站建设预算方案模板
  • 创建网站 英文网站备案 建设方案书
  • 企业公众号以及网站建设iapp网站做软件
  • 体育健身网站建设北京龙鼎网站建设公司
  • 泉州网站建设公司推荐松山湖短视频seo排名
  • 网站分类目录大全wordpress菜单设置中文
  • 简单网站 快速建设网络服务器功能概述
  • 织梦网站程序安装ps做网站显示内容参考
  • 做个网站要多久做网站网站是什么案件
  • 九江市住房和城乡建设局网站wordpress 网页加速
  • 快速模仿一个网站注册公司费用及流程
  • 东莞网站建设服务商福州市建设工程招投标信息网
  • 丝网外贸做哪些网站中等职业学校示范建设专题网站
  • 常州网站建设乛薇国外优秀电商设计网站
  • 家政服务网站源码郑州区块链数字钱包网站开发公司
  • 网站建设与熊掌号未来的关系图书翻页的动画 做网站启动用
  • 清远建设网站网站维护中是不是关闭网站了
  • 三明市建设局网站官网视觉asp网站源码
  • 做外贸建网站需要推广吗东莞商城网站推广建设
  • 网站怎么自己编辑模块关键词排名优化价格
  • 网站建设公司 待遇美橙网站产品详情