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

公司网站建设中心镇江网站建设制作公司

公司网站建设中心,镇江网站建设制作公司,做响应网站,使用 私有云 做视频网站watch() 一共可以接受三个参数,侦听数据源、回调函数和配置选项 作用:监视数据的变化(和Vue2中的watch作用一致) 特点:Vue3中的watch只能监视以下四种数据: ref定义的数据。 reactive定义的数据。 函数返…

watch() 一共可以接受三个参数,侦听数据源、回调函数和配置选项

作用:监视数据的变化(和Vue2中的watch作用一致)

特点:Vue3中的watch只能监视以下四种数据:

ref定义的数据。
reactive定义的数据。
函数返回一个值(getter函数)。
一个包含上述内容的数组
ref 定义的数据

<template><div>str===={{ str }}</div><div>perosn 的name===={{ personObj.name }}</div><div>Person 的age===={{ personObj.age }}</div><div>Person的Car===={{ personObj.car }}</div><div>Person的Car.color===={{ personObj.car.color }}</div><div>Person的Car.price===={{ personObj.car.price }}</div><div><el-button @click="editPerson">点击修改部分属性</el-button><el-button @click="editPersonAll">点击修改全部</el-button><el-button @click="editString">修改基本数据类型</el-button></div>
</template><script setup lang="ts">
import { reactive, watch, ref, toRefs, Ref } from "vue";
interface Person {name: string;age: number;car: {price: number;color: string;};
}
// let str: Ref<string> = ref("AAA"); // 泛型写法
let str = ref("AAA");
let personObj = ref<Person>({name: "aaa",age: 12,car: {price: 12,color: "reds",},
});
const editPerson = () => {personObj.value.name = "bbb";
};
const editPersonAll = () => {personObj.value.car = {price: 222,color: "blue",};
};const editString = () => {str.value = "Bbb";
};
// 监听基本数据类型变化
watch(() => str.value,(newValue, oldValue) => {console.log("监听基本数据类型变化newValue");console.log(newValue);console.log(oldValue);}
);
// 监听对象类型某个值的变化
watch(() => personObj.value.name,(newValue, oldValue) => {console.log("personObj.value.name");console.log("newValue==" + newValue);console.log("oldValue==" + oldValue);}
);/* 监视,情况一:监视【ref】定义的【对象类型】数据,监视的是对象的地址值,若想监视对象内部属性的变化,需要手动开启深度监视watch的第一个参数是:被监视的数据watch的第二个参数是:监视的回调watch的第三个参数是:配置对象(deep、immediate等等.....) */
watch(personObj,(newValue, oldValue) => {console.log("修改整个车");console.log("newValue==");let { car, name, age } = toRefs(newValue);console.log(car, name.value, age.value);console.log("oldValue==");// let {car,name,age} = toRefs(oldValue)// console.log(car,name,age);},{deep: true,}
);
</script><style scoped></style>

reactive 定义的数据
ref 写法

<template><div>perosn 的name===={{ personObj.name }}</div><div>Person 的age===={{ personObj.age }}</div><div>Person的Car===={{ personObj.car }}</div><div>Person的Car.color===={{ personObj.car.color }}</div><div>Person的Car.price===={{ personObj.car.price }}</div><div><el-button @click="editPerson">点击修改部分属性</el-button><el-button @click="editPersonAll">点击修改全部</el-button></div>
</template>
<script setup lang="ts">
import { reactive, watch, ref } from "vue";
interface Person {name: string;age: number;car: {price: number;color: string;};
}
let personObj = reactive<Person>({name: "aaa",age: 12,car: {price: 12,color: "reds",},
});
const editPerson = () => {personObj.name = "bbb";
};
const editPersonAll = () => {personObj.car = {price: 222,color: "blue",};
};
//监听基本数据类型的写法
watch(() => personObj.name,(newValue, oldValue) => {console.log("newValue==" + newValue);console.log("oldValue==" + oldValue);}
);
监听对象类型的写法 (推荐使用这种方法)
// watch(
//   () => personObj.car,
//   (newValue, oldValue) => {
//     console.log("修改整个车");
//     console.log("newValue==" + newValue);
//     console.log("oldValue==" + oldValue);
//   }
// );
监听对象类型的写法
watch(personObj.car, (newValue, oldValue) => {console.log("修改整个车");console.log("newValue==" + newValue);console.log("oldValue==" + oldValue);
});
</script>

监听多个值变化
ref 写法

<template><div>perosn 的name===={{ personObj.name }}</div><div>Person 的age===={{ personObj.age }}</div><div>Person的Car===={{ personObj.car }}</div><div>Person的Car.color===={{ personObj.car.color }}</div><div>Person的Car.price===={{ personObj.car.price }}</div><div><el-button @click="editPersonName">点击修改name</el-button><el-button @click="editPersonCarColor">点击修改car-color</el-button></div>
</template><script setup lang="ts">
import { tr } from "element-plus/es/locales.mjs";
import { reactive, watch, ref, toRefs, Ref } from "vue";
interface Person {name: string;age: number;car: {price: number;color: string;};
}
let personObj = ref<Person>({name: "aaa",age: 12,car: {price: 12,color: "reds",},
});
const editPersonName = () => {personObj.value.name = "bbb";
};
const editPersonCarColor = () => {personObj.value.car.color = "bule";
};// 监听对象类型某个值的变化
// 传入的是数组, 获取到的newValue 也是数组,一一对应的关系
watch([() => personObj.value.name, personObj.value.car],(newValue, oldValue) => {console.log("personObj.value--watch");console.log(newValue);console.log(oldValue);},{deep: true,}
);
</script><style scoped></style>

reactive 写法

<template><div>perosn 的name===={{ personObj.name }}</div><div>Person 的age===={{ personObj.age }}</div><div>Person的Car===={{ personObj.car }}</div><div>Person的Car.color===={{ personObj.car.color }}</div><div>Person的Car.price===={{ personObj.car.price }}</div><div><el-button @click="editPersonName">点击修改name</el-button><el-button @click="editPersonCarColor">点击修改car-color</el-button></div>
</template><script setup lang="ts">
import { tr } from "element-plus/es/locales.mjs";
import { reactive, watch, ref, toRefs, Ref } from "vue";
interface Person {name: string;age: number;car: {price: number;color: string;};
}
let personObj = reactive<Person>({name: "aaa",age: 12,car: {price: 12,color: "reds",},
});
const editPersonName = () => {personObj.name = "bbb";
};
const editPersonCarColor = () => {personObj.car.color = "bule";
};// 监听对象类型某个值的变化
// 传入的是数组, 获取到的newValue 也是数组,一一对应的关系
watch([() => personObj.name, personObj.car],(newValue, oldValue) => {console.log("personObj.value--watch");console.log(newValue);console.log(oldValue);},{deep: true,}
);
</script><style scoped></style>
http://www.hkea.cn/news/578451/

相关文章:

  • wordpress手动裁剪seo营销推广服务公司
  • 英文网站建设网站海南网站制作公司
  • 网页设计与网站建设主要内容软文营销的特点
  • 一起做网站17广州最新小组排名
  • 最专业的网站设计公司有哪些论坛企业推广
  • 单页网站怎么做外链个人网页
  • 宁波城乡住房建设局网站有效的网络推广
  • 网站建设 深圳销售crm客户管理系统
  • 高端网站开发设计站长之家字体
  • 免费网站建站工具购买域名的网站
  • 淘宝联盟怎么做网站百度网站提交
  • 前端做用vue做后台多还是做网站多青岛网站快速排名优化
  • 岳阳网站开发公司海淀区seo多少钱
  • 2017年做网站维护总结百度搜索软件
  • 南京网站建设公司点击器原理
  • 网站怎么编辑搜狗网站提交入口
  • 自建网站做外贸的流程广告推广方式
  • 警告欺骗网站模板免费注册
  • 获取网站访客信息seo分析师招聘
  • 制作网页的网站有哪些网站建设
  • 日本真人做爰无遮挡视频免费网站嘉兴关键词优化报价
  • 忻州市中小企业局网站贵州整站优化seo平台
  • 网页怎么制作超链接seo兼职接单平台
  • 网站建设中应注意哪些问题重庆整站seo
  • 贵阳网站建设哪家便宜微商软文范例大全100
  • 怎么在微信上做网站竞价交易
  • wordpress优化版4.7.4网站seo设计
  • 网上课程网站精准客户数据采集软件
  • 专业网站建设报价外呼系统电销
  • 网站建设公司价格差别seo还有哪些方面的优化