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

Vue3組件間的通信方式詳解

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

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

1、父子組件通信

1.1 defineProps

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

具體實現(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="請輸入">
      <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>

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

1.2 provide/inject

當我們聊完了props,我們第二個要介紹的就是 vue3 的一個組合式選項 provide 和inject。

projct用于提供可以被后代組件注入的值,而inject用于聲明要通過從上層提供方匹配并注入進當前組件的屬性。 其代碼實現(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="請輸入">
        <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向外提供了一個 key 為 list,值為list.value,同時將 list,value 設(shè)置成了只讀屬性,防止子組件修改父組件的數(shù)據(jù)源。然后我們 injectAPI接收了 list,實現(xiàn)了父子組件的通信。

2.子父組件通信

2.1 defineEmits

上面我介紹了兩種父向子傳值的方法,但在我們開發(fā)中,我們還會遇到子向父組件傳值的情況,那我們該怎么解決呢? 第一個方法就是vue3中的 defineEmits API,代碼實現(xiàn)如下:

// children.vue
<template>
    <div class="parent-wrap">
        <input type="text" v-model="value" class="form-control" placeholder="請輸入" />
        <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']) //父傳子
  // 給父組件傳一個函數(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>

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

2.2 v-model:xxx+emit

在介紹完 defineEmits后, 我們再來介紹一種與其有異曲同工之處的v-model:xxx + emit的方法,實現(xiàn)如下:

// children.vue
<template>
    <div class="parent-wrap">
      <input type="text" v-model="value" class="form-control" placeholder="請輸入" />
        <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'])
  // 給父組件一點東西
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ù)源,同時再emit還給父組件,實現(xiàn)了子組件向父組件傳值。

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

相關(guān)文章

  • Vue項目結(jié)合Vue-layer實現(xiàn)彈框式編輯功能(實例代碼)

    Vue項目結(jié)合Vue-layer實現(xiàn)彈框式編輯功能(實例代碼)

    這篇文章主要介紹了Vue項目中結(jié)合Vue-layer實現(xiàn)彈框式編輯功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Vue自定義指令實現(xiàn)彈窗拖拽四邊拉伸及對角線拉伸效果

    Vue自定義指令實現(xiàn)彈窗拖拽四邊拉伸及對角線拉伸效果

    小編最近在做一個vue前端項目,需要實現(xiàn)彈窗的拖拽,四邊拉伸及對角線拉伸,以及彈窗邊界處理功能,本文通過實例代碼給大家分享我的實現(xiàn)過程及遇到問題解決方法,感興趣的朋友一起看看吧
    2021-08-08
  • Vue 2.0在IE11中打開項目頁面空白的問題解決

    Vue 2.0在IE11中打開項目頁面空白的問題解決

    這篇文章主要給大家介紹了關(guān)于Vue 2.0在ie 11中打開項目頁面空白問題的解決方法,文中詳細分析出現(xiàn)該問題的原因,并給出了詳細的解決方法,需要的朋友可以參考借鑒,下面跟著小編來一起學習學習吧。
    2017-07-07
  • vue實現(xiàn)放大鏡效果

    vue實現(xiàn)放大鏡效果

    這篇文章主要為大家詳細介紹了vue實現(xiàn)放大鏡效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • vue3中?provide?和?inject?用法及原理

    vue3中?provide?和?inject?用法及原理

    這篇文章主要介紹vue3中?provide?和?inject?用法及原理,provide?和?inject可以?解決多次組件傳遞數(shù)據(jù)的問題,下面文章是具體的用法和實現(xiàn)原理,具有一定的參考價值,需要的朋友可以參考一下,希望對大家有所幫助
    2021-11-11
  • vant-image本地圖片無法顯示的解決方式

    vant-image本地圖片無法顯示的解決方式

    這篇文章主要介紹了vant-image本地圖片無法顯示的解決方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Vue3中defineEmits、defineProps?不用引入便直接用

    Vue3中defineEmits、defineProps?不用引入便直接用

    這篇文章主要介紹了Vue3中defineEmits、defineProps?不用引入便直接用,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • 解決vue3中使用echart報錯:Cannot read properties of undefined (reading ‘type‘)

    解決vue3中使用echart報錯:Cannot read properties of&n

    在Vue項目中使用Echarts進行數(shù)據(jù)可視化是非常常見的需求,然而有時候在引入Echarts的過程中可能會遇到報錯,本文主要介紹了解決vue3中使用echart報錯:Cannot read properties of undefined (reading ‘type‘),感興趣的可以了解一下
    2024-01-01
  • vue項目移動端實現(xiàn)ip輸入框問題

    vue項目移動端實現(xiàn)ip輸入框問題

    這篇文章主要介紹了vue項目移動端實現(xiàn)ip輸入框問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • 用vue2.0實現(xiàn)點擊選中active其他選項互斥的效果

    用vue2.0實現(xiàn)點擊選中active其他選項互斥的效果

    這篇文章主要介紹了用vue2.0實現(xiàn)點擊選中active其他選項互斥的效果,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04

最新評論