vue2和vue3子組件父組件之間的傳值方法
先看一下vue2
- 父組件向子組件傳遞參數(shù)
父組件通過
:語法 其實(shí)就是v-bind 來傳遞參數(shù)
子組件通過props來獲取父組件傳遞的方法
億點(diǎn)小知識:子組件接收到數(shù)據(jù)之后,不能直接修改父組件的數(shù)據(jù)。會報(bào)錯(cuò)
// 父組件 parent 像子組件傳遞 msg 值
<template>
<Children :datum="'我是父組件的數(shù)據(jù)'"></Children>
</template>
?----------------------------------------------------------------------------------
// 子組件 接收 父組件 傳遞的數(shù)據(jù)
export default {
// 寫法一 用數(shù)組接收
props:['datum'],
// 寫法二 用對象接收,可以限定接收的數(shù)據(jù)類型、設(shè)置默認(rèn)值、驗(yàn)證等
props:{
datum:{
type:String,
default:'這是默認(rèn)數(shù)據(jù)'
}
},
mounted(){
console.log(this.datum)// 我是父組件的數(shù)據(jù)
},
}- 子組件向父組件傳遞參數(shù) (這里同時(shí)講了父組件向子組件傳遞方法)
父組件通過
@語法 其實(shí)就是v-on 來傳遞方法
子組件通過$emit來獲取父組件傳遞的方法 同時(shí)向父組件傳遞數(shù)據(jù)
<template>
<Children @method="method"></Children>
</template>
<script>
import Children from './Children';
export default {
components: {
Children
},
methods: {
method(data) { // 這里的 data 就是子組件傳遞的參數(shù) 如果參數(shù)擁有多個(gè)可以使用 ...語法獲取參數(shù)
console.log(data);// 子組件傳遞的參數(shù)
}
}
};
</script>
?----------------------------------------------------------------------------------
// 子組件 傳遞給 父組件數(shù)據(jù)
export default {
methods: {
childMethod() { // 子組件通過 $emit 獲取父組件傳遞的方法,然后攜帶數(shù)據(jù)傳給父組件
this.$emit('method',"我是子組件");
}
}
}- 父組件使用子組件的方法
vue2.0里面父組件調(diào)用子組件的方法是通過
$refs實(shí)現(xiàn)的
//父組件
<template>
<Children ref="child"></Children>
</template>
export default{
import Children from './Children'
export default{
components:{
Children
},
mounted:{
//調(diào)用子組件方法 這里要注意區(qū)分 child 是ref的名字
this.$refs.child.getData(val) //通過$refs找到子組件,并找到方法執(zhí)行
}
}
}以上就是 vue2 子組件父組件之間的通訊
vue3
相信能看懂 vue2的小伙伴 應(yīng)該理解之間的通訊 這里我就直接在父組件和子組件進(jìn)行通訊
- 父組件
<template>
<Children :title="我是父組件" ref="childrenRef" @method="methodChildren"></Children >
</template>
<script lang="ts">
import Children from "./Children.vue"
import { defineComponent, ref } from "vue"
export default defineComponent({
components: {
Children ,
},
setup() {
let msg = ref("")
let childrenRef = ref() // 通過ref獲取 子組件的實(shí)例
let fun = () =>{
childrenRef.value.fatherFun()// 使用子組件的方法
}
let methodChildren = (val) => {
msg.value = val // 這里val獲取子組件傳遞的值
}
return {
msg,
methodChildren,
}
},
})
</script>- 子組件
<template>
<!-- 點(diǎn)擊調(diào)用父組件的方法 -->
<button @click="fatherMethod">點(diǎn)擊</button>
</template>
<script lang="ts">
import { defineComponent } from "vue"
export default defineComponent({
name: "Children",
props: {
title: {
type: String,
},
},
setup(props, {emit}) {
const fatherMethod= () => {
emit("method", "傳值給父組件")
}
const fatherFun= () => {
console.log("我是子組件的方法")
}
return {
fatherMethod,
}
},
})
</script>以上就是vue2和vue3子組件父組件之間的傳值方法的詳細(xì)內(nèi)容,更多關(guān)于vue2和vue3組件傳值的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
mpvue中使用flyjs全局?jǐn)r截的實(shí)現(xiàn)代碼
這篇文章主要介紹了mpvue中使用flyjs全局?jǐn)r截的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
基于vue實(shí)現(xiàn)swipe輪播組件實(shí)例代碼
本篇文章主要介紹了基于vue實(shí)現(xiàn)swipe輪播組件實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
npm?ERR!?code?E404在vscode安裝插件時(shí)報(bào)錯(cuò)的兩種解決方案
這篇文章主要給大家介紹了關(guān)于npm?ERR!?code?E404在vscode安裝插件時(shí)報(bào)錯(cuò)的兩種解決方案,關(guān)于這個(gè)問題,通常是由于插件名稱輸入錯(cuò)誤、網(wǎng)絡(luò)問題或插件已被刪除引起的,文中將兩種解決方法都介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
Vue3圖片上傳報(bào)錯(cuò):Required?part?‘file‘?is?not?present.的原因及解決方法
這篇文章主要介紹了Vue3圖片上傳報(bào)錯(cuò):Required?part?‘file‘?is?not?present.的原因及解決方法,文中通過代碼示例講解的非常詳細(xì),對大家解決問題有一定的幫助,需要的朋友可以參考下2024-09-09
vue2與vue3中生命周期執(zhí)行順序的區(qū)別說明
這篇文章主要介紹了vue2與vue3中生命周期執(zhí)行順序的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

