深入了解Vue3組件傳值方式
今天說一下 vue3 的組件間傳值,學(xué)習過 vue2 的寶子們肯定知道,組件傳值是 vue 項目開發(fā)過程中必不可少的功能場景,在 vue2 里面有很多傳值的方式,vue3 的傳值方式呢,在這里稍微整理總結(jié)一下,但是不是很全,后期可能慢慢補充。
父子組件傳值 props
和 vue2 一樣,vue3 也可以使用 props 進行父組件傳值給子組件,這個就不多說了直接上代碼。
父組件
<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>這是父組件傳進的數(shù)據(jù): {{msg}}</h2>
<h2>這是父組件傳進的數(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 = "我是子組件傳進的值";
emit("change", news);
};
return { msg, boy, btn };
}
};
</script>
<style scoped>
.son {
background-color: bisque;
}
</style>保存查看效果。

上面就是 props 傳值的基本用法。
祖孫組件傳值 provide 和 inject
這個其實和 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>這是父組件傳進的數(shù)據(jù): {{boy.name}}</h2>
<h2>這是父組件傳進的數(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>刷新看一下效果。

好的,我們可以看到子組件可以順利拿到父組件傳進來的數(shù)據(jù)值。
父組件中點擊按鈕向子組件傳值
就是使用 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對象傳進去
}
return { boy, btn, hello };
}
};
</script>
<style scoped>
.father {
background-color: aquamarine;
}
</style>子組件
<template>
<div class="son">
<h1>這是子組件</h1>
<h2>這是父組件傳進的數(shù)據(jù): {{boy.name}}</h2>
<h2>這是父組件傳進的數(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組件傳值方式的詳細內(nèi)容,更多關(guān)于Vue3組件傳值的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue中keep-alive內(nèi)置組件緩存的實例代碼
這篇文章主要介紹了vue中的keep-alive內(nèi)置組件緩存,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Vue組合式API--setup中定義響應(yīng)式數(shù)據(jù)的示例詳解
在Vue2.x中,編寫組件的方式是使用Options API,它的特點是在對應(yīng)的屬性中編寫對應(yīng)的功能模塊,這篇文章主要介紹了Vue組合式API--setup中定義響應(yīng)式數(shù)據(jù)詳解,需要的朋友可以參考下2022-10-10
element-ui循環(huán)顯示radio控件信息的方法
今天小編就為大家分享一篇element-ui循環(huán)顯示radio控件信息的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
vue動態(tài)綁定class的幾種常用方式小結(jié)
這篇文章主要介紹了vue動態(tài)綁定class的幾種常用方式,結(jié)合實例形式總結(jié)分析了vue.js常見的對象方法、數(shù)組方法進行class動態(tài)綁定相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
vue項目中data數(shù)據(jù)之間互相訪問的實現(xiàn)
本文主要介紹了vue項目中data數(shù)據(jù)之間互相訪問的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2022-05-05

