vue watch

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

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

father.vue 复制代码
<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>
son.vue 复制代码
	<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里面的监听的,所以这里建议使用深拷贝
在线代码

相关推荐
老华带你飞2 分钟前
社区养老保障|智慧养老|基于springboot+小程序社区养老保障系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序·毕设·社区养老保障
Danny_FD5 分钟前
用 ECharts markLine 标注节假日
前端·echarts
程序员西西6 分钟前
SpringBoot无感刷新Token实战指南
java·开发语言·前端·后端·计算机·程序员
烛阴6 分钟前
Luban集成CocosCreator完整教程
前端·typescript·cocos creator
有点笨的蛋6 分钟前
深入理解 JavaScript 原型机制:构造函数、原型对象与原型链
前端·javascript
o***74178 分钟前
spring-boot-starter和spring-boot-starter-web的关联
前端
晴栀ay10 分钟前
JS中原型式面向对象的精髓
前端·javascript
3秒一个大10 分钟前
JavaScript 原型详解:从概念到实践
javascript
美幻10 分钟前
前端复制功能在移动端失效?一文彻底搞懂 Clipboard API 的兼容性陷阱
前端
llxxyy卢12 分钟前
XSS跨站之订单及shell箱子反杀记
前端·xss