欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue使用watch監(jiān)聽props的技巧分享

 更新時(shí)間:2023年12月08日 10:56:48   作者:剛學(xué)HTML  
這篇文章主要為大家詳細(xì)介紹了vue使用watch監(jiān)聽props的一些小建議,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

vue 使用watch監(jiān)聽props的一些小建議

當(dāng)在watch里面給data賦值,請(qǐng)使用深拷貝。

<template>
  <div class="container">
    <div class="left">
      <div class="button_group">
        <!--        <button @click="random_change_data">修改某一列的數(shù)據(jù)</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() {
    //   /**
    //    * 隨機(jī)修改某一列的數(shù)據(jù)
    //    */
    //   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: [], // 選擇的數(shù)據(jù)
      data: [], // 表格數(shù)據(jù)
    };
  },
  props: {
    table_data: {
      type: Array,
      default: () => [],
    },
    choice_list: {
      type: Array,
      default: () => [],
    },
  },
  watch: {
    choice_list: {
      handler(new_: [string], old_) {
        console.log("choice_list 發(fā)生了改變");
        /**
         * 根據(jù)名字去判斷是否選擇
         */
        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>

這個(gè)時(shí)候如果修改data里面的值,是會(huì)觸發(fā)watch里面的監(jiān)聽的,所以這里建議使用深拷貝

在線代碼

到此這篇關(guān)于vue使用watch監(jiān)聽props的技巧分享的文章就介紹到這了,更多相關(guān)vue watch監(jiān)聽props內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue實(shí)現(xiàn)公共方法抽離

    vue實(shí)現(xiàn)公共方法抽離

    這篇文章主要介紹了vue實(shí)現(xiàn)公共方法抽離,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Element-UI表格嵌入popover遇到的問題及解決方案

    Element-UI表格嵌入popover遇到的問題及解決方案

    在表格中我們通常需要在每一行的一些單元格中顯示popover,這篇文章主要給大家介紹了關(guān)于Element-UI表格嵌入popover遇到的問題及解決方案,需要的朋友可以參考下
    2023-11-11
  • vue Tab切換以及緩存頁面處理的幾種方式

    vue Tab切換以及緩存頁面處理的幾種方式

    這篇文章主要介紹了vue Tab切換以及緩存頁面處理的幾種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • vue-element-admin登錄全流程分享

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

    這篇文章主要介紹了vue-element-admin登錄全流程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 在Vue中使用WebScoket?會(huì)怎么樣?

    在Vue中使用WebScoket?會(huì)怎么樣?

    這篇文章主要介紹了在Vue中使用WebScoket,網(wǎng)絡(luò)的不穩(wěn)定而斷開連接,webscoket不會(huì)出現(xiàn)異常,下面就一起進(jìn)入文章看看吧
    2022-01-01
  • Vue?Router?返回后記住滾動(dòng)條位置的實(shí)現(xiàn)方法

    Vue?Router?返回后記住滾動(dòng)條位置的實(shí)現(xiàn)方法

    使用?Vue?router?創(chuàng)建?SPA(Single?Page?App),往往有這種需求:首頁是列表頁,點(diǎn)擊列表項(xiàng)進(jìn)入詳情頁,在詳情頁點(diǎn)擊返回首頁后,希望看到的是,首頁不刷新,并且滾動(dòng)條停留在之前的位置,這篇文章主要介紹了Vue?Router?返回后記住滾動(dòng)條位置的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2023-09-09
  • Vue.js報(bào)錯(cuò)Failed to resolve filter問題的解決方法

    Vue.js報(bào)錯(cuò)Failed to resolve filter問題的解決方法

    這篇文章主要介紹了Vue.js報(bào)錯(cuò)Failed to resolve filter問題的解決方法,需要的朋友可以參考下
    2016-05-05
  • Vue-CLI多頁分目錄打包的步驟記錄

    Vue-CLI多頁分目錄打包的步驟記錄

    這篇文章主要給大家介紹了關(guān)于Vue-CLI多頁分目錄打包的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 關(guān)于Element-ui中el-table出現(xiàn)的表格錯(cuò)位問題解決

    關(guān)于Element-ui中el-table出現(xiàn)的表格錯(cuò)位問題解決

    使用ElementUI的el-table后,偶然發(fā)現(xiàn)出現(xiàn)行列錯(cuò)位、對(duì)不齊問題,下面這篇文章主要給大家介紹了關(guān)于Element-ui中el-table出現(xiàn)的表格錯(cuò)位問題解決的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • Vue項(xiàng)目中實(shí)現(xiàn)帶參跳轉(zhuǎn)功能

    Vue項(xiàng)目中實(shí)現(xiàn)帶參跳轉(zhuǎn)功能

    最近做了一個(gè)手機(jī)端系統(tǒng),其中遇到了父頁面需要攜帶參數(shù)跳轉(zhuǎn)至子頁面的問題,現(xiàn)已解決,下面分享一下實(shí)現(xiàn)過程,感興趣的朋友一起看看吧
    2021-04-04

最新評(píng)論