VUE3(四)、組件通信

1、props

? ? ? ? 作用:子組件之間的通信。
父傳子:屬性值的非函數。
子傳父:屬性值是函數。

? ? ? ?父組件:

<template><div>{{ childeData }}</div>——————————————————————————————<child :parentData="parentData" :sendData="getChildData"/>
</template>
<script setup lang="ts" name="parentIndex">
import { ref } from 'vue';
import child from './childeIndex.vue'const childeData=ref("");
const parentData=ref("parentData");function getChildData(data:string){childeData.value=data;
}</script>

? ? ? ? 子組件:

<template><div>{{ parentData }}</div><button @click="sendData(data)"></button>
</template>
<script setup lang="ts" name="child">
import { ref } from 'vue'
defineProps(['parentData','sendData'])const data=ref('aaa')</script>

2、自定義事件

? ? ? ? 通過父組件定義函數,子組件調用父組件的函數來實現參數的傳遞。

????????父組件:

<template><div>{{ childeData }}</div>——————————————————————————————<child @send-toy="getChildData"/>
</template>
<script setup lang="ts" name="parentIndex">
import { ref } from 'vue';
import child from './childeIndex.vue'const childeData=ref("");function getChildData(data:string){childeData.value=data;
}</script>

? ? ? ? 子組件:

<template><div>子組件</div><button @click="sendDat"></button>
</template>
<script setup lang="ts" name="child">import { ref } from 'vue'let data=ref('我是子組件')const emit = defineEmits(['send-toy'])function sendDat(){emit('send-toy',data)}</script>

3、mitt

????????實現任意組件的通信。個人理解:在組件與組件之間提供了一個平臺,都向這個平臺發送、讀取數據。
安裝步驟:

  1. npm i mitt
  2. 新建文件utiles/emitter.ts
  3. 在需要的文件中引入emitter,進行綁定 事件,或調用事件。
  4. ?在文件中進行解綁(unmounted中)

? ? ? ?emitter.ts

import mitt from 'mitt'
const emitter=mitt()
export default emitter

? ? ? ? 獲取數據的文件

<template><h2>child2中的數據:{{ src }}</h2>
</template>
<script setup lang="ts" name="child1">import { onUnmounted, ref } from 'vue'import emitter from '@/utils/emitter'const src=ref('')//監聽事件emitter.on('getData',(data:string)=>{src.value=data})//在組件銷毀時取消監聽onUnmounted(()=>{emitter.off('getData')})
</script>

? ? ? ? 發送數據的文件

<template><h2>child2的數據:{{ data }}</h2><button @click="getdata"> 點擊獲取數據 </button>
</template>
<script setup lang="ts" name="child2">import { ref } from 'vue'import emitter from '@/utils/emitter'const data = ref('222')function getdata(){emitter.emit('getData',data)}
</script>

4、v-model

? ? ? ? 原理:實現雙向綁定,通過傳遞props參數實現父傳子,通過emit傳遞input或者點擊事件實現子傳父。

? ? ? ? 父組件:

<template><!-- 這個寫法的本質就是下面那種 --><input type="text" v-model="str"/><input type="text" :value="str" @input="str=(<HTMLInputElement>$event.target).value"/><!-- 自定義組件,一二種方法一致 --><selfInput v-model="str"></selfInput><!-- 此處的$event就是傳遞的數據 --><selfInput :modelValue="str" @update:modelValue="str=$event"></selfInput></template>
<script setup lang="ts" name="parentIndex">
import { ref } from 'vue';
import selfInput from './selfInput.vue';let str = ref('');
</script>

? ? ? ? 自定義組件(selfInput.vue):

<template><input type="text" :value="modelValue" @input="emit('update:modelValue',($event.target as HTMLInputElement).value)"/>
</template>
<script setup lang="ts" >
import { ref,defineProps,defineEmits } from 'vue';defineProps(['modelValue'])const emit = defineEmits(['update:modelValue'])</script>

如果自定義自定義組件中的名稱,可以實現多個組件的雙向綁定。

? ? ? ? 父組件:

<template>{{ str }} --------{{ pass }}<selfInput v-model:name="str" v-model:pass="pass"></selfInput> 
</template>
<script setup lang="ts" name="parentIndex">
import { ref } from 'vue';
import selfInput from './selfInput.vue';let str = ref('');
let pass = ref('');
</script>

? ? ? ? 子組件:

<template><input type="text" :value="name" @input="emit('update:name',($event.target as HTMLInputElement).value)"/><input type="text" :value="pass" @input="emit('update:pass',($event.target as HTMLInputElement).value)"/>
</template>
<script setup lang="ts" >
import { ref,defineProps,defineEmits } from 'vue';defineProps(['name','pass'])const emit = defineEmits(['update:name','update:pass'])</script>

5、$attrs

? ? ? ? 作用:適用于當前組件,向子組件通信(祖-->孫)。
當父組件給子組件傳遞參數,且子組件沒有使用props進行接收時,所傳遞的參數存在attrs中。在子組件中使用$attrs就可以獲取所有未被接收的數據。

? ? ? ? 父組件:

<template><div>{{ a }}</div><hr/><div>{{ b }}</div><hr/><div>{{ c }}</div><hr/><child v-bind="{a,b,c}" @changeA="changeA"></child>
</template>
<script setup lang="ts" name="parentIndex">
import { ref } from 'vue';
import child from './chile.vue';
let a=ref(1);
let b=ref(2);
let c=ref(3);
function changeA(value:number){a.value=value;
}
</script>

? ? ? ? 子組件:

<template><div>{{ a }}</div><hr/>--------------------------------<sun v-bind="$attrs"></sun>
</template>
<script setup lang="ts" name="chile">import sun from './sun.vue'defineProps(['a'])
</script>

? ? ? ? 孫組件:

<template><div>{{ b }}</div><hr/><div>{{ c }}</div><hr/><button @click="changeAa">修改a</button>
</template>
<script setup lang="ts" >const num=defineProps(['b','c']);const emit=defineEmits(['changeA']);function changeAa(){emit('changeA',num.b);}
</script>

6、$refs、$parent

? ? ? ? $refs:用于父組件修改子組件中的數據。獲取所有子組件暴露的數據。
$parent:用于子組件修改父組件的數據。獲取父組件所有暴露的數據。

????????以上的前提:使用defineExpose暴露數據。

? ? ? ? 父組件:

<template><h1>父組件數據</h1><h1>{{ a }}</h1><button @click="changeChildeA($refs)">修改子組件數據</button><br/>--------------------------------<child1 ref="child1"/>--------------------------------<child2 ref="child2"/>
</template>
<script setup lang="ts" name="parentIndex">
import { ref } from 'vue';
import child1 from './childe1.vue';
import child2 from './childe2.vue';
let a=ref(1);
function changeChildeA(refs:any){refs.child1.b++;refs.child2.b++;
}
defineExpose({a})
</script>

? ? ? ? 子組件1:

<template>    <h1>子組件數據</h1><h1>a:{{ a }}</h1><h1>b:{{ b }}</h1><button @click="changeParent($parent)">修改父組件的a</button>
</template>
<script setup lang="ts" >
import { ref } from 'vue'
let a=ref(1)
let b=ref(1)
function changeParent(parent:any){parent.a++
}
//暴露
defineExpose({b
})
</script>

? ? ? ? 子組件2:

<template>    <h1>子組件數據</h1><h1>a:{{ a }}</h1><h1>b:{{ b }}</h1>
</template>
<script setup lang="ts" >
import { ref } from 'vue'
let a=ref(1)
let b=ref(1)//暴露
defineExpose({a,b
})
</script>

7、provide、inject

? ? ? ? 作用:實現祖孫組件之間的數據傳遞。
傳遞數據:provide(“數據名稱”,數據)。
接收數據:inject(“數據名稱”,默認值)。

? ? ? ? 父組件:

<template><h1>父組件數據</h1><h1>{{ a }}</h1>--------------------------------<child1 ref="child1"/>
</template>
<script setup lang="ts" name="parentIndex">
import { ref,provide } from 'vue';
import child1 from './childe1.vue';let a=ref(0);
function changeA(value:number){a.value+=value;
}provide("parent",{a:a,changeA})</script>

? ? ? ? 子組件:

<template>    <h1>子組件數據</h1><h1>a:{{ a }}</h1><div><button @click="changeA(1)">更改父組件的a</button></div>
</template>
<script setup lang="ts" >
import { ref,inject } from 'vue'
let {a,changeA} = inject('parent',{a:ref(0),changeA:function(x:number){}})
</script>

8、插槽

? ? ? ? 作用:引入組件時可以給組件傳遞一些HTML元素。? ? ? ??

默認插槽

? ? ? ? 父組件中引入子組件時,子組件標簽內部的元素就是會傳入slot的參數。
子組件中的slot就相當于占位符,給未來傳進來的元素預留空位。

? ? ? ? 父組件:

<template><h1>父組件數據</h1><div class="child"><child1 :title="'第一個'"><div style="color: aqua;">1111111111</div></child1><child1 :title="'第二個'"><div style="color:black;">22</div></child1></div>
</template>
<script setup lang="ts" name="parentIndex">
import child1 from './childe.vue';
</script>
<style scoped>

? ? ? ? 子組件:

    <div class="child"><h1 style="color: black;">{{ title }}</h1><slot>默認值</slot></div></template>
<script setup lang="ts" >defineProps(['title'])
</script>

具名插槽

? ? ? ? 作用:可以指定元素插入的位置。
用法:在定義插槽位置時,使用name屬性定義其名字。
在應用組件時,在其標簽或Templet標簽內部定義v-slot(或使用#+名字)屬性。

? ? ? ? 父組件:? ? ??

<template><h1>父組件數據</h1><div class="child"><child1 :title="'第一個'"><template #b><div style="color: aqua;">1111111111</div></template></child1><child1 :title="'第二個'"><template #a><div style="color:black;">22</div></template></child1></div>
</template>
<script setup lang="ts" name="parentIndex">
import child1 from './childe.vue';
</script>

? ? ? ? 子組件:

<template>    <div class="child"><h1 style="color: black;">{{ title }}</h1><slot name="a">默認值</slot><h1>-------------分隔符--------------------</h1><slot name="b">默認值</slot></div></template>
<script setup lang="ts" >defineProps(['title'])
</script>

作用域插槽

? ? ? ? 作用:當父組件需要子組件的數據。
用法:在定義slot標簽時,使用props寫法進行傳遞參數。
在父組件中使用v-slot拿到參數。
如果同時使用了具名插槽:v-slot:name=‘parms’? ? ? ? ? ? ? ?

? ? ? ? 父組件:

<template><h1>父組件數據</h1><div class="child"><child1 :title="'第一個'"><template #a="{game,a}"><ul><li v-for="item in game" :key="item.id">{{ item.name }}</li></ul></template></child1></div>
</template>
<script setup lang="ts" name="parentIndex">
import child1 from './childe.vue';
</script>

? ? ? ?子組件:

<template>    <div class="child"><h1 style="color: black;">{{ title }}</h1><slot name="a" :game="game" :a="a">默認值</slot></div></template>
<script setup lang="ts" >
import { reactive } from 'vue'; defineProps(['title'])const game=reactive([{id:1,name:'a'},{id:2,name:'b'},])const a=1;
</script>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/91174.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/91174.shtml
英文地址,請注明出處:http://en.pswp.cn/web/91174.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【數據結構與算法】數據結構初階:詳解二叉樹(六)——二叉樹應用:二叉樹選擇題

&#x1f525;個人主頁&#xff1a;艾莉絲努力練劍 ?專欄傳送門&#xff1a;《C語言》、《數據結構與算法》、C語言刷題12天IO強訓、LeetCode代碼強化刷題 &#x1f349;學習方向&#xff1a;C/C方向 ??人生格言&#xff1a;為天地立心&#xff0c;為生民立命&#xff0c;為…

Android廣播實驗

【實驗目的】了解使用Intent進行組件通信的原理&#xff1b;了解Intent過濾器的原理和匹配機制&#xff1b;掌握發送和接收廣播的方法【實驗內容】任務1、普通廣播&#xff1b;任務2、系統廣播&#xff1b;任務3、有序廣播&#xff1b;【實驗要求】1、練習使用靜態方法和動態方…

html轉word下載

一、插件使用//轉html為wordnpm i html-docx-js //保存文件到本地npm i file-saver 注&#xff1a;vite 項目使用esm模式會報錯&#xff0c;with方法錯誤&#xff0c;修改如下&#xff1a;//直接安裝修復版本npm i html-docx-fixed二、封裝導出 exportWord.jsimport htmlDocx f…

北方公司面試記錄

避免被開盒&#xff0c;先稱之為“北方公司”&#xff0c;有確定結果后再更名。 先說流程&#xff0c;線下面試&#xff0c;時間非常急&#xff0c;下午兩點鐘面試&#xff0c;中午十二點打電話讓我去&#xff0c;帶兩份紙質簡歷。 和一般的菌工單位一樣&#xff0c;先在傳達室…

linux——ps命令

PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND0 1 1 1 ? -1 Ss 0 0:01 /usr/lib/systemd/systemd1 123 123 123 ? -1 S 0 0:00 /usr/sbin/sshd -D123 456 456 456 pts/0 456 R 10…

C#.NET 依賴注入詳解

一、是什么 在 C#.NET 中&#xff0c;依賴注入&#xff08;Dependency Injection&#xff0c;簡稱 DI&#xff09; 是一種設計模式&#xff0c;用于實現控制反轉&#xff08;Inversion of Control&#xff0c;IoC&#xff09;&#xff0c;以降低代碼耦合、提高可測試性和可維護…

Vue監視數據的原理和set()的使用

在 Vue 中&#xff0c;Vue.set()&#xff08;或 this.$set()&#xff09;是用于解決響應式數據更新檢測的重要方法&#xff0c;其底層與 Vue 的數據監視原理緊密相關。以下從使用場景和實現原理兩方面詳細說明&#xff1a;一、Vue.set () 的使用場景與用法1. 為什么需要 Vue.se…

在 Vue 中,如何在回調函數中正確使用 this?

在 Vue 組件中&#xff0c;this 指向當前組件實例&#xff0c;但在回調函數&#xff08;如定時器、異步請求、事件監聽等&#xff09;中&#xff0c;this 的指向可能會丟失或改變&#xff0c;導致無法正確訪問組件的屬性和方法。以下是在回調函數中正確使用 this 的幾種常見方式…

第4章唯一ID生成器——4.4 基于數據庫的自增主鍵的趨勢遞增的唯一ID

基于數據庫的自增主鍵也可以生成趨勢遞增的唯一 ID&#xff0c;且由于唯一ID不與時間戳關聯&#xff0c;所以不會受到時鐘回撥問題的影響。 4.4.1 分庫分表架構 數據庫一般都支持設置自增主鍵的初始值和自增步長&#xff0c;以MySQL為例&#xff0c;自增主鍵的自增步長由auto_i…

設計模式:Memento 模式詳解

Memento 模式詳解Memento&#xff08;備忘錄&#xff09;模式是一種行為型設計模式&#xff0c;用于在不破壞封裝性的前提下&#xff0c;捕獲并外部化一個對象的內部狀態&#xff0c;以便在之后能夠將該對象恢復到原先保存的狀態。它廣泛應用于需要實現撤銷&#xff08;Undo&am…

數據結構(6)單鏈表算法題(下)

一、環形鏈表Ⅰ 1、題目描述 https://leetcode.cn/problems/linked-list-cycle 2、算法分析 思路&#xff1a;快慢指針 根據上圖所示的流程&#xff0c;我們可以推測出這樣一個結論&#xff1a;若鏈表帶環&#xff0c;快慢指針一定會相遇。 那么&#xff0c;這個猜測是否正…

智能制造,從工廠建模,工藝建模,柔性制造,精益制造,生產管控,庫存,質量等多方面講述智能制造的落地方案。

智能制造&#xff0c;從工廠建模&#xff0c;工藝建模&#xff0c;柔性制造&#xff0c;精益制造&#xff0c;生產管控&#xff0c;庫存&#xff0c;質量等多方面講述智能制造的落地方案。

Qt 分裂布局:QSplitter 使用指南

在 GUI 開發中&#xff0c;高效管理窗口空間是提升用戶體驗的關鍵。QSplitter 作為 Qt 的核心布局組件&#xff0c;讓動態分割窗口變得簡單直觀。一、QSplitter 核心功能解析 QSplitter 是 Qt 提供的布局管理器&#xff0c;專用于創建可調節的分割區域&#xff1a; 支持水平/垂…

R語言與作物模型(DSSAT模型)技術應用

R語言在DSSAT模型的氣候、土壤、管理措施等數據準備&#xff0c;自動化模擬和結果分析上都發揮著重要的作用。一&#xff1a;DSSAT模型的高級應用 1.作物模型的概念 2.DSSAT模型發展現狀 3.DSSAT與R語言的安裝 4.DSSAT模型的高級應用案例 5.R語言在作物模型參數優化中的應用 6.…

JavaSE:學習輸入輸出編寫簡單的程序

一、打印輸出到屏幕 Java提供了三種核心輸出方法&#xff0c;適合不同場景&#xff1a; System.out.println() 打印內容后 自動換行 System.out.println("Welcome"); System.out.println("to ISS"); // 輸出&#xff1a; // Welcome // to ISSSystem.out…

訪問者模式感悟

訪問者模式 首先有兩個東西: 一個是訪問者vistor (每一個訪問者類都代表了一類操作) 一個是被訪問者entity (model /info/pojo/node等等這些都行)也就是是說是一個實體類 其操作方法被抽離給了其他類。 訪問者模式的核心思想就是**“把操作從數據結構中分離出來,每種操作…

從零到部署:基于Go和Docker的全棧短鏈接服務實戰(含源碼)

摘要&#xff1a;本文將手把手帶你使用Go語言&#xff0c;并遵循依賴倒置、分層架構等最佳實踐&#xff0c;構建一個高性能、高可用的全棧短鏈接生成器。項目采用Echo框架、GORM、Redis、MySQL&#xff0c;并通過Docker和Docker Compose實現一鍵式容器化部署到阿里云服務器。文…

MyBatis_3

上一篇文章&#xff0c;我們學習了使用XML實現MyBatis進行增、刪、查、改等操作&#xff0c;本篇文章&#xff0c;我們將學習#{ }和${ }獲取方法參數的區別和使用MyBatisXML實現動態SQL語句。 #{ }和${ }的區別 在之前的文章中我們都是使用#{ }進行賦值&#xff0c;但實際上M…

智能圖書館管理系統開發實戰系列(一):項目架構設計與技術選型

項目背景 智能圖書館管理系統&#xff08;ILMS&#xff09;是一個現代化的桌面應用程序&#xff0c;采用前后端分離架構&#xff0c;結合了Web技術的靈活性和桌面應用的用戶體驗。本項目從高保真原型設計開始&#xff0c;經過完整的軟件開發生命周期&#xff0c;最終實現為一個…

應急前端“黃金3分鐘”設計:極端場景下的操作界面極速搭建技術

摘要**地震突發&#xff0c;應急指揮系統的操作界面卻因加載緩慢無法及時調取數據&#xff1b;火災現場&#xff0c;消防員手持終端的操作步驟繁瑣&#xff0c;延誤救援時機。在分秒必爭的極端場景中&#xff0c;傳統前端操作界面為何頻頻 “掉鏈子”&#xff1f;怎樣才能在 “…