vue3 父子組件傳值詳解
現(xiàn)在距離vue3的誕生已經(jīng)過了很長時(shí)間了,筆者也是近期才開始學(xué)習(xí)vue3。對(duì)比vue2來看,vue3在寫法發(fā)生了不小的變化,最典型的例子就是vue3通過ref,或者reactive實(shí)現(xiàn)數(shù)據(jù)的響應(yīng)式。因?yàn)閞ef和reactive的出現(xiàn),使得vue3中父子組件的傳值方式也發(fā)生了變化
咱們先看下vue2中的寫法
父組件:
<!-- 父組件 -->
<template>
<div>
<children :title="title" @getChildren="getChildren"></children>
<div>子組件說: {{ childrenAsk }}</div>
</div>
</template>
<script>
import children from "./children.vue"
export default {
data() {
return {
title: "我是父組件傳過來的值",
childrenAsk: ""
}
},
methods: {
getChildren(val) {
this.childrenAsk = val
}
}
}
</script>
子組件:
<!-- 子組件 -->
<template>
<div>
<div>父組件傳過來的值: {{ title }}</div>
<button @click="askToFather">點(diǎn)擊發(fā)送給父組件</button>
</div>
</template>
<script>
export default {
props: {
title: {
type: String
}
},
data() {
return {
askMsg: "這是我給父組件說的話"
}
},
methods: {
askToFather() {
this.$emit("getChildren", this.askMsg)
}
}
}
</script>
在vue2中,子組件向父組件傳值通過this.$emit的形式實(shí)現(xiàn),但是vue3中,是不存在this的,vue3中將數(shù)據(jù)和函數(shù)都封裝在了setup中,那么vue3是怎么實(shí)現(xiàn)的呢?
我們知道vue3中的setup接收兩個(gè)參數(shù),第一個(gè)參數(shù)是props,即父組件向子組件傳遞的props值,第二個(gè)值為context,這個(gè)值代表了當(dāng)前的上下文對(duì)象,知道這個(gè)東西以后現(xiàn)在來實(shí)現(xiàn)vue3的父子組件傳值
vue3中父?jìng)髯雍蛌ue2中的父?jìng)髯右粯?,再次不做過多闡述,下面重點(diǎn)關(guān)注的是vue3的子傳父
父組件
<template>
<div style="color: aqua">父組件</div>
<div>子組件對(duì)我說:{{ children_msg }}</div>
<children :title="msg" @listen="listenToChildren"></children>
{{ value }}
</template>
<script lang="ts">
import children from "@/views/component_emit/children.vue"
import { defineComponent, ref } from "vue"
export default defineComponent({
components: {
children,
},
name: "father",
setup() {
let msg = "我是父組件"
let children_msg = ref("") // ref的作用是實(shí)現(xiàn)響應(yīng)式, 如果沒有ref則不能實(shí)現(xiàn)響應(yīng)式(引用數(shù)據(jù)類型用reactive)
let listenToChildren = (val) => {
children_msg.value = val // 使用ref包裹的數(shù)據(jù),需要通過.value的形式訪問他的值
}
return {
msg,
children_msg,
listenToChildren,
}
},
})
</script>
<style></style>
子組件:
<template>
<div style="color: brown">子組件</div>
<!-- 父?jìng)髯邮褂梅椒ê蛌ue2相同 -->
<div>父組件傳過來的值為:{{ title }}</div>
<button @click="sayToFather">向父組件說話</button>
</template>
<script lang="ts">
import { defineComponent } from "vue"
export default defineComponent({
name: "children",
props: {
title: {
type: String,
},
},
setup(props, context) {
// context作用是獲取上下文對(duì)象,
// 如果setup寫法為setup(props, { emit })的方式的話,下面的context可以省略
const sayToFather = () => {
const ask = "我是子組件,我對(duì)父組件說話"
context.emit("listen", ask)
}
return {
sayToFather,
}
},
})
</script>
<style></style>
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
vue iview組件表格 render函數(shù)的使用方法詳解
下面小編就為大家分享一篇vue iview組件表格 render函數(shù)的使用方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03
vue實(shí)現(xiàn)從外部修改組件內(nèi)部的變量的值
這篇文章主要介紹了vue實(shí)現(xiàn)從外部修改組件內(nèi)部的變量的值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
vue-router中 query傳參和params傳參的使用和區(qū)別講解
這篇文章主要介紹了vue-router中 query傳參和params傳參的使用和區(qū)別講解,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01
解決vue-seamless-scroll滾動(dòng)加點(diǎn)贊銜接處數(shù)據(jù)不同步問題
這篇文章主要介紹了解決vue-seamless-scroll滾動(dòng)加點(diǎn)贊銜接處數(shù)據(jù)不同步問題,初步判斷可能是因?yàn)橄路綉医觱ue-seamless-scroll是靜態(tài)的,沒同步DOM,本文給大家分享解決方法,感興趣的朋友一起看看吧2021-11-11
Vue3.x源碼調(diào)試的實(shí)現(xiàn)方法
這篇文章主要介紹了Vue3.x源碼調(diào)試的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Vue3.0?axios跨域請(qǐng)求代理服務(wù)器配置方式
這篇文章主要介紹了Vue3.0?axios跨域請(qǐng)求代理服務(wù)器配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue自定義指令結(jié)合阿里云OSS優(yōu)化圖片的實(shí)現(xiàn)方法
這篇文章主要介紹了Vue自定義指令結(jié)合阿里云OSS優(yōu)化圖片的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

