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

vue watch

vue 使用watch监听props的一些小建议

当在watch里面给data赋值,请使用深拷贝。

<template><div class="container"><div class="left"><div class="button_group"><!--        <button @click="random_change_data">修改某一列的数据</button>--></div></div><div class="right son"><son_component :table_data="table_data"></son_component></div></div>
</template><script lang="ts">
import Vue from "vue";
import son_component from "@/components/son_component.vue";export default Vue.extend({name: "FatherComponent",components: {son_component,},data() {return {table_data: [],};},mounted() {this.init_data();},methods: {init_data() {for (let i = 0; i < 100; i++) {(this.table_data as never as [{ name: string; age: number; check: boolean }]).push({name: `alex${i}`,age: i,check: false,});}console.log(this.table_data);},generate_random_number(max: number) {return Math.floor(Math.random() * max) + 1;},// random_change_data() {//   /**//    * 随机修改某一列的数据//    *///   const index = this.generate_random_number(this.table_data.length);//   // (this.table_data[index] as { age: number }).age = 100;//   const item = this.table_data[index] as { age: number };//   item.age = 100;//   this.$set(this, index, item);// },},
});
</script>
<style scoped>
.container {display: flex;flex-direction: row;width: 100vw;
}.left,
.right {width: 50vw;
}.left {margin: 0 auto;line-height: 100%;text-align: center;
}
</style>
	<template><div><div class="table_data"><table><thead><tr><th>名字</th><th>年龄</th><th><input type="checkbox" v-model="is_all" /></th></tr></thead><tbody><tr v-for="item in data" :key="item.name"><td>{{ item.name }}</td><td>{{ item.age }}</td><td><input type="checkbox" v-model="item.check" /></td></tr></tbody></table></div></div>
</template>
<script lang="ts">
import Vue from "vue";
import { cloneDeep } from "lodash";export default Vue.extend({name: "son_component",data() {return {is_all: true,selection: [], // 选择的数据data: [], // 表格数据};},props: {table_data: {type: Array,default: () => [],},choice_list: {type: Array,default: () => [],},},watch: {choice_list: {handler(new_: [string], old_) {console.log("choice_list 发生了改变");/*** 根据名字去判断是否选择*/if (new_) (this.selection as any) = this.choice_list.concat(new_);},immediate: true,deep: true,},table_data: {handler(new_) {(this.data as any) = this.table_data;}},},
});
</script><style scoped></style>

这个时候如果修改data里面的值,是会触发watch里面的监听的,所以这里建议使用深拷贝

在线代码

http://www.lryc.cn/news/255066.html

相关文章:

  • 异常检测 | 基于孤立森林(Isolation Forest)的数据异常数据检测(结合t-SNE降维可视化)
  • 【深度学习】一维数组的聚类
  • 100多种视频转场素材|专业胶片,抖动,光效电影转场特效PR效果预设
  • http与apache
  • 一、服务器准备
  • 区块链optimism主网节点搭建
  • Bounding boxes augmentation for object detection
  • 【计算机网络学习之路】HTTP请求
  • java之字符串常用处理函数
  • 【XILINX】ERROR:Place:1136 - This design contains a global buffer instance
  • 【文件上传系列】No.0 利用 FormData 实现文件上传、监控网路速度和上传进度(原生前端,Koa 后端)
  • web前端之JavaScrip的笔试题
  • 生活、工作常用API免费接口
  • PHP使用mkcert本地开发生成HTTPS证书 PhpEnv集成环境
  • DHTMLX Scheduler PRO 6.0.5 Crack
  • AddressSanitizer和LeakSanitizer有什么区别
  • CoreDNS实战(二)-CoreDNS+ETCD实现DNS服务发现
  • B站缓存视频M4S合并MP4(js + ffmpeg )
  • 学习IO的第四天
  • Linux:缓冲区的概念理解
  • 中山大学李华山、王彪课题组开发 SEN 机器学习模型,高精度预测材料性能
  • 二百一十、Hive——Flume采集的JSON数据文件写入Hive的ODS层表后字段的数据残缺
  • 【开放集检测OSR】开放集检测和闭集检测的区别和联系:从模型角度进行理解
  • 【Flink】Flink核心概念简述
  • linux-tar命令、解压、压缩
  • Kafka Connect :构建强大分布式数据集成方案
  • 基于 Flink CDC 构建 MySQL 的 Streaming ETL to MySQL
  • 创建vue项目:node.js下载安装、配置环境变量,下载安装cnpm,配置npm的目录、镜像,安装vue、搭建vue项目开发环境(保姆级教程一)
  • uni-app 微信小程序之好看的ui登录页面(一)
  • [ES]ElasticSearch强转日期的时区问题