深入了解Vue3組件傳值方式
今天說一下 vue3 的組件間傳值,學(xué)習(xí)過 vue2 的寶子們肯定知道,組件傳值是 vue 項(xiàng)目開發(fā)過程中必不可少的功能場景,在 vue2 里面有很多傳值的方式,vue3 的傳值方式呢,在這里稍微整理總結(jié)一下,但是不是很全,后期可能慢慢補(bǔ)充。
父子組件傳值 props
和 vue2 一樣,vue3 也可以使用 props 進(jìn)行父組件傳值給子組件,這個就不多說了直接上代碼。
父組件
<template> <div> <div class="father"> <h1>這是父組件</h1> <h2>父組件的名字:{{boy.name}}</h2> </div> <hello-world :msg="msg" :boy="boy" @change="btn"></hello-world> </div> </template> <script> import HelloWorld from "./components/HelloWorld.vue"; import { ref, reactive } from "vue"; export default { name: "App", components: { HelloWorld }, setup() { const boy = reactive({ name: "我是????.", age: 10 }); const msg = ref("這是一條信息"); const btn = val => { console.log(val); alert(val); }; return { boy, msg, btn }; } }; </script> <style scoped> .father { background-color: aquamarine; } </style>
子組件
<template> <div class="son"> <h1>這是子組件</h1> <h2>這是父組件傳進(jìn)的數(shù)據(jù): {{msg}}</h2> <h2>這是父組件傳進(jìn)的數(shù)據(jù): {{boy.name}}</h2> <button @click="btn">傳給父組件數(shù)據(jù)</button> </div> </template> <script> import { toRefs } from "vue"; export default { name: "HelloWorld", props: { msg: String, boy: Object }, setup(props, { emit }) { console.log(props); const p = toRefs(props); const msg = p.msg; const boy = p.boy; const btn = () => { const news = "我是子組件傳進(jìn)的值"; emit("change", news); }; return { msg, boy, btn }; } }; </script> <style scoped> .son { background-color: bisque; } </style>
保存查看效果。
上面就是 props 傳值的基本用法。
祖孫組件傳值 provide 和 inject
這個其實(shí)和 vue2 的寫法是一模一樣的。
直接上代碼??!
父組件
<template> <div> <div class="father"> <h1>這是父組件</h1> <h2>名字:{{boy.name}}</h2> <h2>年齡:{{boy.age}}</h2> </div> <hello-world></hello-world> </div> </template> <script> import HelloWorld from "./components/HelloWorld.vue"; import { reactive, provide } from "vue"; export default { name: "App", components: { HelloWorld }, setup() { const boy = reactive({ name: "我是????.", age: 10 }); provide("boy", boy); // 往子孫組件傳值 return { boy }; } }; </script> <style scoped> .father { background-color: aquamarine; } </style>
子組件
<template> <div class="son"> <h1>這是子組件</h1> <h2>這是父組件傳進(jìn)的數(shù)據(jù): {{boy.name}}</h2> <h2>這是父組件傳進(jìn)的數(shù)據(jù): {{boy.age}}</h2> </div> </template> <script> import { toRefs, inject } from "vue"; export default { name: "HelloWorld", setup() { const boy = inject("boy"); // 子孫組件接收值 return { boy }; } }; </script> <style scoped> .son { background-color: bisque; } </style>
刷新看一下效果。
好的,我們可以看到子組件可以順利拿到父組件傳進(jìn)來的數(shù)據(jù)值。
父組件中點(diǎn)擊按鈕向子組件傳值
就是使用 ref 來獲取dom 然后操作里面的參數(shù)和方法。
父組件
<template> <div> <div class="father"> <h1>這是父組件</h1> <h2>名字:{{boy.name}}</h2> <h2>年齡:{{boy.age}}</h2> <button @click="btn">傳值</button> </div> <hello-world ref="hello" style="color: red"></hello-world> </div> </template> <script> import HelloWorld from "./components/HelloWorld.vue"; import { reactive, ref } from "vue"; export default { name: "App", components: { HelloWorld }, setup() { const boy = reactive({ name: "我是????.", age: 10 }); const hello = ref(); function btn() { hello.value.init(boy); // 調(diào)用子組件的方法,把boy對象傳進(jìn)去 } return { boy, btn, hello }; } }; </script> <style scoped> .father { background-color: aquamarine; } </style>
子組件
<template> <div class="son"> <h1>這是子組件</h1> <h2>這是父組件傳進(jìn)的數(shù)據(jù): {{boy.name}}</h2> <h2>這是父組件傳進(jìn)的數(shù)據(jù): {{boy.age}}</h2> </div> </template> <script> import { reactive, toRefs } from "vue"; export default { name: "HelloWorld", setup() { let boy = reactive({ name: "ed.", age: 11 }); // 提供給父組件調(diào)用的方法 const init = val => { boy.name = val.name; boy.age = val.age; }; return { init, boy }; } }; </script> <style scoped> .son { background-color: bisque; } </style>
以上就是深入了解Vue3組件傳值方式的詳細(xì)內(nèi)容,更多關(guān)于Vue3組件傳值的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue中keep-alive內(nèi)置組件緩存的實(shí)例代碼
這篇文章主要介紹了vue中的keep-alive內(nèi)置組件緩存,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04Vue組合式API--setup中定義響應(yīng)式數(shù)據(jù)的示例詳解
在Vue2.x中,編寫組件的方式是使用Options API,它的特點(diǎn)是在對應(yīng)的屬性中編寫對應(yīng)的功能模塊,這篇文章主要介紹了Vue組合式API--setup中定義響應(yīng)式數(shù)據(jù)詳解,需要的朋友可以參考下2022-10-10Vue3封裝組件完整實(shí)例(帶回調(diào)事件)
Vue.js已成為現(xiàn)代Web開發(fā)中不可或缺的技術(shù)之一,雖然Vue.js的一些基礎(chǔ)概念和語法比較易學(xué),但深入挖掘Vue.js的核心概念和功能需要更多的實(shí)踐,下面這篇文章主要給大家介紹了關(guān)于Vue3封裝組件(帶回調(diào)事件)的相關(guān)資料,需要的朋友可以參考下2023-06-06element-ui循環(huán)顯示radio控件信息的方法
今天小編就為大家分享一篇element-ui循環(huán)顯示radio控件信息的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08Vue.js中的extend綁定節(jié)點(diǎn)并顯示的方法
在本篇內(nèi)容里小編給大家整理了關(guān)于Vue.js中的extend綁定節(jié)點(diǎn)并顯示的方法以及相關(guān)知識點(diǎn),需要的朋友們學(xué)習(xí)下。2019-06-06vue動態(tài)綁定class的幾種常用方式小結(jié)
這篇文章主要介紹了vue動態(tài)綁定class的幾種常用方式,結(jié)合實(shí)例形式總結(jié)分析了vue.js常見的對象方法、數(shù)組方法進(jìn)行class動態(tài)綁定相關(guān)操作技巧,需要的朋友可以參考下2019-05-05vue項(xiàng)目中data數(shù)據(jù)之間互相訪問的實(shí)現(xiàn)
本文主要介紹了vue項(xiàng)目中data數(shù)據(jù)之間互相訪問的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05vue使用canvas實(shí)現(xiàn)移動端手寫簽名
這篇文章主要為大家詳細(xì)介紹了基于vue使用canvas實(shí)現(xiàn)移動端手寫簽名,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-09-09