最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue3組件間的通信方式詳解

 更新時(shí)間:2023年04月24日 09:29:46   作者:憂郁的蛋~  
這篇文章主要介紹了Vue3組件間的通信方式,在使用vue時(shí),我們經(jīng)常會(huì)把不同的模塊拆分成不同的組件,而組件之間有的需要傳遞數(shù)據(jù),所以組件間的數(shù)據(jù)通信就非常重要了

在寫 vue3 的項(xiàng)目中,我們都會(huì)進(jìn)行組件通信,除了使用 pinia 公共數(shù)據(jù)源的方式除外,我們還可采用那些更簡(jiǎn)單的API方法呢?給大家介紹介紹幾種父子組件和子父組件通信的方式。

1、父子組件通信

1.1 defineProps

父子組件通信我們第一個(gè)想到的就是props,我們?cè)谧咏M件顯示聲明所接受的props,然后我們?cè)趶母附M件傳入對(duì)應(yīng)的 key與value, 這樣我們就可以在子組件上接收到父組件傳過來的屬性與值。

具體實(shí)現(xiàn)如下:

// children.vue
<template>
  <ul class="list-group">
      <li class="list-group-item" v-for="item in list" :key="index">
        {{item}}
      </li>
  </ul>
</template>
<script setup>
import { defineProps } from 'vue';
const props = defineProps({
    list :{
        type: Array,
        default: () => {}
    }
})
</script>
// parent.vue
<template>
  <div class="parent-wrap">
      <input type="text" v-model="value" class="form-control" placeholder="請(qǐng)輸入">
      <div class="input-group-append">
          <button class="btn btn-primary" @click="handleAdd">添加</button>
      </div>
  </div>
  <!-- child -->
  <childrenVue :list="list"></childrenVue>
</template>
<script setup>
import { ref } from 'vue';
import childrenVue from './children.vue';
const value = ref('')
const list = ref(['javaScript', 'Html', 'CSS'])
const handleAdd = () =>{
    list.value.push(value.value)
    value = ''
}
</script>

如上圖所示,我們既實(shí)現(xiàn)了在子組件上顯示了父組件傳過來的 list 數(shù)組,還使可以向list添加數(shù)據(jù)使子組件數(shù)據(jù)更新。

1.2 provide/inject

當(dāng)我們聊完了props,我們第二個(gè)要介紹的就是 vue3 的一個(gè)組合式選項(xiàng) provide 和inject。

projct用于提供可以被后代組件注入的值,而inject用于聲明要通過從上層提供方匹配并注入進(jìn)當(dāng)前組件的屬性。 其代碼實(shí)現(xiàn)如下:

// children.vue
<template>
    <ul class="list-group">
        <li class="list-group-item" v-for="item in list" :key="item">{{item}}</li>
    </ul>
</template>
<script setup>
import { inject } from 'vue';
const list = inject('list')
</script>
// parent.vue
<template>
    <div class="parent-wrap">
        <input type="text" v-model="value" class="form-control" placeholder="請(qǐng)輸入">
        <div class="input-group-append">
            <button class="btn btn-primary" @click="handleAdd">添加</button>
        </div>
    </div>
    <!-- child -->
    <childVue />
</template>
<script setup>
import childVue from "./child.vue";
const { ref, provide, readonly } = require("vue");
const value = ref('')
const list = ref(['javaScript', 'HTML', 'CSS'])
provide('list', readonly(list.value))
const handleAdd = () => {
list.value.push(value.value)
}
</script>

如上圖所示,我們使用 provide API向外提供了一個(gè) key 為 list,值為list.value,同時(shí)將 list,value 設(shè)置成了只讀屬性,防止子組件修改父組件的數(shù)據(jù)源。然后我們 injectAPI接收了 list,實(shí)現(xiàn)了父子組件的通信。

2.子父組件通信

2.1 defineEmits

上面我介紹了兩種父向子傳值的方法,但在我們開發(fā)中,我們還會(huì)遇到子向父組件傳值的情況,那我們?cè)撛趺唇鉀Q呢? 第一個(gè)方法就是vue3中的 defineEmits API,代碼實(shí)現(xiàn)如下:

// children.vue
<template>
    <div class="parent-wrap">
        <input type="text" v-model="value" class="form-control" placeholder="請(qǐng)輸入" />
        <div class="input-group-append">
            <button class="btn btn-primary" @click="handleAdd">添加</button>
        </div>
    </div>
</template>
<script setup>
const { ref, defineEmits } = require("vue");
const value = ref('')
const emits = defineEmits(['add']) //父?jìng)髯?
  // 給父組件傳一個(gè)函數(shù)
const handleAdd = () => {
    emits('add', value.value)
    value.value= ''
}
</script>
// parent.vue
<template>  
    <childVue @add='handleAdd'/>
    <ul class="list-group">
        <li class="list-group-item" v-for="item in list" :key="item">{{item}}</li>
    </ul>
</template>
<script setup>
import { ref } from '@vue/reactivity';
import childVue from './child.vue';
const list = ref(['javaScript', 'HTML', 'CSS'])
const handleAdd = (val) => {
    list.value.push(val)
}
</script>

如上圖所示,我們?cè)谧咏M件上emit一個(gè)出了一個(gè) add事件給父組件接收,同時(shí)在父組件上調(diào)用來執(zhí)行添加的邏輯,再將 inputvalue變?yōu)榭?,?shí)現(xiàn)了父組件向子組件傳參。

2.2 v-model:xxx+emit

在介紹完 defineEmits后, 我們?cè)賮斫榻B一種與其有異曲同工之處的v-model:xxx + emit的方法,實(shí)現(xiàn)如下:

// children.vue
<template>
    <div class="parent-wrap">
      <input type="text" v-model="value" class="form-control" placeholder="請(qǐng)輸入" />
        <div class="input-group-append">
            <button class="btn btn-primary" @click="handleAdd">添加</button>
        </div>
    </div>
</template>
<script setup>
const { ref, defineProps, defineEmits } = require("vue");
const value = ref('')
const props = defineProps({
    list: {
        type: Array,
        default: () => []
    }
})
const emits = defineEmits(['list'])
  // 給父組件一點(diǎn)東西
const handleAdd = () => {
    // props.list.push(value.value)  //不建議直接修改props的值 把握不住數(shù)據(jù)源的流轉(zhuǎn)
    const arr = props.list
    arr.push(value.value)
    emits('list', arr)
    value.value= ''
}
</script>
<template>  
    <childVue v-model:list="list" @list ='add'/>
    <ul class="list-group">
        <li class="list-group-item" v-for="item in list" :key="item">{{item}}</li>
    </ul>
</template>
<script setup>
import { ref } from '@vue/reactivity';
import childVue from './child.vue';
const list = ref(['javaScript', 'HTML', 'CSS'])
const add =(val) => {
    console.log(val);
    console.log(list);
}
</script>

再和上面的defineEmits方法比較完以后,相信大家也看出了這兩者的異曲同工在哪了。我們這里是先將父組件的list傳給了子組件,再在子組件修改了父組件的數(shù)據(jù)源,同時(shí)再emit還給父組件,實(shí)現(xiàn)了子組件向父組件傳值。

到此這篇關(guān)于Vue3組件間的通信方式詳解的文章就介紹到這了,更多相關(guān)Vue3組件通信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue 中如何利用 new Date() 獲取當(dāng)前時(shí)間

    Vue 中如何利用 new Date() 獲取當(dāng)前時(shí)間

    在 Vue 開發(fā)中,利用 new Date() 方法可以方便地獲取當(dāng)前時(shí)間,并通過 Date 對(duì)象的方法進(jìn)行時(shí)間格式化和操作。通過本文的介紹,您應(yīng)該對(duì)在 Vue 中獲取當(dāng)前時(shí)間有了更深入的了解,并了解了一些常見的時(shí)間操作方法,需要的朋友可以參考下
    2023-07-07
  • 解決VUE中document.body.scrollTop為0的問題

    解決VUE中document.body.scrollTop為0的問題

    今天小編就為大家分享一篇解決VUE中document.body.scrollTop為0的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • Vue項(xiàng)目配置router.js流程分析講解

    Vue項(xiàng)目配置router.js流程分析講解

    第一次寫Vue項(xiàng)目,要用到router.js,看了一下官方文檔,還是很懵逼,不知道怎么配置,又去看視頻查資料,最后終于搞定了。話不多說,先上代碼,我再講一些要注意的細(xì)節(jié)
    2022-12-12
  • vue的無(wú)縫滾動(dòng)組件vue-seamless-scroll實(shí)例

    vue的無(wú)縫滾動(dòng)組件vue-seamless-scroll實(shí)例

    本篇文章主要給大家講解了vue的無(wú)縫滾動(dòng)組件vue-seamless-scroll的用法,需要的朋友參考學(xué)習(xí)下吧。
    2017-12-12
  • vue中如何覆蓋style中的樣式

    vue中如何覆蓋style中的樣式

    在Vue組件中覆蓋樣式,可以使用更高優(yōu)先級(jí)的選擇器、!important或深度選擇器(如::v-deep)來實(shí)現(xiàn),選擇合適的方法取決于具體需求
    2025-01-01
  • VUE實(shí)現(xiàn)自身整體組件銷毀的示例代碼

    VUE實(shí)現(xiàn)自身整體組件銷毀的示例代碼

    這篇文章主要介紹了VUE實(shí)現(xiàn)自身整體組件銷毀的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • vue2.0實(shí)現(xiàn)檢測(cè)無(wú)用的代碼并刪除

    vue2.0實(shí)現(xiàn)檢測(cè)無(wú)用的代碼并刪除

    這篇文章主要介紹了vue2.0實(shí)現(xiàn)檢測(cè)無(wú)用的代碼并刪除方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • vue移動(dòng)端時(shí)彈出側(cè)邊抽屜菜單效果

    vue移動(dòng)端時(shí)彈出側(cè)邊抽屜菜單效果

    這篇文章主要介紹了vue移動(dòng)端時(shí)彈出側(cè)邊抽屜菜單,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • Vue兩個(gè)版本的區(qū)別和使用方法(更深層次了解)

    Vue兩個(gè)版本的區(qū)別和使用方法(更深層次了解)

    在我們使用 vue時(shí),我們可以引用兩個(gè)不同版本的 Vue,分別是 Vue完整版(vue.js)和 Vue(vue.runtime.js )非完整版,那么它們的區(qū)別是什么呢,今天我們就來分析下這兩個(gè)不同版本之間的區(qū)別,一起看看吧
    2020-02-02
  • Vue使用Post進(jìn)行表單提交

    Vue使用Post進(jìn)行表單提交

    這篇文章主要介紹了Vue使用Post進(jìn)行表單提交,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06

最新評(píng)論

秦安县| 百色市| 江阴市| 弋阳县| 英吉沙县| 雅安市| 绿春县| 新竹县| 大连市| 迁安市| 辽阳市| 通道| 尼玛县| 皮山县| 兰州市| 府谷县| 攀枝花市| 永顺县| 泰和县| 阿鲁科尔沁旗| 敦化市| 左云县| 乐东| 武鸣县| 霍邱县| 新竹市| 荥阳市| 定南县| 怀安县| 绥阳县| 兰考县| 岚皋县| 穆棱市| 九寨沟县| 定南县| 周至县| 赣榆县| 海丰县| 文安县| 齐河县| 丹巴县|