vue使用watch监听props的技巧分享

 更新时间:2023年12月08日 10:56:48   作者:刚学HTML  
这篇文章主要为大家详细介绍了vue使用watch监听props的一些小建议,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下

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里面的监听的,所以这里建议使用深拷贝

在线代码

到此这篇关于vue使用watch监听props的技巧分享的文章就介绍到这了,更多相关vue watch监听props内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vue3使用slot插槽的实现

    Vue3使用slot插槽的实现

    插槽在真实的开发中使用非常的多,比如我们去用一些第三方组件库的时候,通常都需要通过自定义插槽来实现内容的自定义,本文主要介绍了Vue3使用slot插槽的实现,感兴趣的可以了解一下
    2023-12-12
  • Vue和SpringBoot之间传递时间的方法实现

    Vue和SpringBoot之间传递时间的方法实现

    本文主要介绍了Vue和SpringBoot之间传递时间的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • Vue3源码分析组件挂载创建虚拟节点

    Vue3源码分析组件挂载创建虚拟节点

    这篇文章主要为大家介绍了Vue3源码分析组件挂载创建虚拟节点,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • Vue.js中使用iView日期选择器并设置开始时间结束时间校验功能

    Vue.js中使用iView日期选择器并设置开始时间结束时间校验功能

    本文通过实例代码给大家介绍了Vue.js中使用iView日期选择器并设置开始时间结束时间校验功能,需要的朋友可以参考下
    2018-08-08
  • 前端vue中实现文件下载的几种方法总结

    前端vue中实现文件下载的几种方法总结

    这篇文章主要介绍了前端vue中实现文件下载的几种方法总结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • Vue中axios的基本用法详解

    Vue中axios的基本用法详解

    axios 是一个基于promise用于浏览器和 nodejs 的 HTTP 客户端,这篇文章主要介绍了Vue中axios的基本用法及axios的特征和使用注意细节,本文通过示例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • Vue Input输入框自动获得焦点的有效方法

    Vue Input输入框自动获得焦点的有效方法

    我们有时候会遇到要输入框自动获取焦点的情况,下面这篇文章主要给大家介绍了关于Vue Input输入框自动获得焦点的简单方法,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-11-11
  • Vue实现页面的局部刷新(router-view页面刷新)

    Vue实现页面的局部刷新(router-view页面刷新)

    本文主要介绍了Vue实现页面的局部刷新(router-view页面刷新),文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-12-12
  • 详解Vue3.x中组件间参数传递的示例代码

    详解Vue3.x中组件间参数传递的示例代码

    在 Vue3.x 中,组件间的参数传递是构建复杂应用时不可或缺的一部分,无论是父子组件还是兄弟组件之间,合理的数据流动都是保持应用状态一致性和可维护性的关键,本文将通过示例代码,详细介绍 Vue3.x 中组件间如何传递参数,需要的朋友可以参考下
    2024-03-03
  • vue实现导航栏下拉菜单

    vue实现导航栏下拉菜单

    这篇文章主要为大家详细介绍了vue实现导航栏下拉菜单,带展开收缩动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-09-09

最新评论