vue2和vue3子組件父組件之間的傳值方法
先看一下vue2
- 父組件向子組件傳遞參數(shù)
父組件通過
:
語法 其實(shí)就是v-bind 來傳遞參數(shù)
子組件通過props
來獲取父組件傳遞的方法
億點(diǎn)小知識:子組件接收到數(shù)據(jù)之后,不能直接修改父組件的數(shù)據(jù)。會(huì)報(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)文章
vue項(xiàng)目強(qiáng)制清除頁面緩存的例子
今天小編就為大家分享一篇vue項(xiàng)目強(qiáng)制清除頁面緩存的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11vue input輸入框關(guān)鍵字篩選檢索列表數(shù)據(jù)展示
這篇文章主要為大家詳細(xì)介紹了vue input輸入框關(guān)鍵字篩選檢索列表數(shù)據(jù)展示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05vuex + keep-alive實(shí)現(xiàn)tab標(biāo)簽頁面緩存功能
這篇文章主要介紹了vuex + keep-alive實(shí)現(xiàn)tab標(biāo)簽頁面緩存功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10Vuex中的getter和mutation的區(qū)別詳解
在現(xiàn)代前端開發(fā)中,狀態(tài)管理是一個(gè)不可忽視的話題,而Vuex作為Vue.js的官方狀態(tài)管理庫,在大型應(yīng)用中扮演著至關(guān)重要的角色,當(dāng)我們使用Vuex進(jìn)行狀態(tài)管理時(shí),getter和mutation是兩個(gè)重要的概念,在本文中,我們將詳細(xì)探討getter和mutation的區(qū)別,需要的朋友可以參考下2024-12-12Vue項(xiàng)目如何部署到Tomcat服務(wù)器上
這篇文章主要介紹了Vue項(xiàng)目如何部署到Tomcat服務(wù)器上,Vue中自帶webpack,可以通過一行命令將項(xiàng)目打包,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-03-03vue.js利用Object.defineProperty實(shí)現(xiàn)雙向綁定
這篇文章主要為大家詳細(xì)介紹了vue.js利用Object.defineProperty實(shí)現(xiàn)雙向綁定,幫大家解析神秘的Object.defineProperty方法2017-03-03