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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue实现公共方法抽离

    vue实现公共方法抽离

    这篇文章主要介绍了vue实现公共方法抽离,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • Element-UI表格嵌入popover遇到的问题及解决方案

    Element-UI表格嵌入popover遇到的问题及解决方案

    在表格中我们通常需要在每一行的一些单元格中显示popover,这篇文章主要给大家介绍了关于Element-UI表格嵌入popover遇到的问题及解决方案,需要的朋友可以参考下
    2023-11-11
  • vue Tab切换以及缓存页面处理的几种方式

    vue Tab切换以及缓存页面处理的几种方式

    这篇文章主要介绍了vue Tab切换以及缓存页面处理的几种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • vue-element-admin登录全流程分享

    vue-element-admin登录全流程分享

    这篇文章主要介绍了vue-element-admin登录全流程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • 在Vue中使用WebScoket 会怎么样?

    在Vue中使用WebScoket 会怎么样?

    这篇文章主要介绍了在Vue中使用WebScoket,网络的不稳定而断开连接,webscoket不会出现异常,下面就一起进入文章看看吧
    2022-01-01
  • Vue Router 返回后记住滚动条位置的实现方法

    Vue Router 返回后记住滚动条位置的实现方法

    使用 Vue router 创建 SPA(Single Page App),往往有这种需求:首页是列表页,点击列表项进入详情页,在详情页点击返回首页后,希望看到的是,首页不刷新,并且滚动条停留在之前的位置,这篇文章主要介绍了Vue Router 返回后记住滚动条位置的实现方法,需要的朋友可以参考下
    2023-09-09
  • Vue.js报错Failed to resolve filter问题的解决方法

    Vue.js报错Failed to resolve filter问题的解决方法

    这篇文章主要介绍了Vue.js报错Failed to resolve filter问题的解决方法,需要的朋友可以参考下
    2016-05-05
  • Vue-CLI多页分目录打包的步骤记录

    Vue-CLI多页分目录打包的步骤记录

    这篇文章主要给大家介绍了关于Vue-CLI多页分目录打包的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • 关于Element-ui中el-table出现的表格错位问题解决

    关于Element-ui中el-table出现的表格错位问题解决

    使用ElementUI的el-table后,偶然发现出现行列错位、对不齐问题,下面这篇文章主要给大家介绍了关于Element-ui中el-table出现的表格错位问题解决的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • Vue项目中实现带参跳转功能

    Vue项目中实现带参跳转功能

    最近做了一个手机端系统,其中遇到了父页面需要携带参数跳转至子页面的问题,现已解决,下面分享一下实现过程,感兴趣的朋友一起看看吧
    2021-04-04

最新评论