分享Vue子組件接收父組件傳值的3種方式
父組件代碼↓
<template>
?? ?<div>
?? ??? ?<div>父組件</div>
?? ??? ?<Student :name="name" :age="age"></Student>
?? ?</div>
</template>
<script>
?? ?// 引入組件
?? ?import Student from './components/Student'
?? ?
?? ?export default {
?? ??? ?name: 'App',
?? ??? ?components: {Student,},
?? ??? ?data() {
?? ??? ??? ?return {
?? ??? ??? ??? ?name: "張三",
?? ??? ??? ??? ?age: 18
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ?}
?? ?
</script>1.簡(jiǎn)單聲明接收
<template>
?? ?<div>
?? ??? ?<div>子組件</div>
?? ??? ?<h2>學(xué)生姓名:{{ name }}</h2>
?? ??? ?<h2>年齡:{{ age }}</h2>
?? ?</div>
</template>
<script>
?? ?export default {
?? ??? ?name: 'Student',
?? ??? ?data() {
?? ??? ??? ?return {}
?? ??? ?},
?? ??? ?
?? ??? ?// 簡(jiǎn)單聲明接收 ----------
?? ??? ?props: ['name', 'age']
?? ??? ?// ---------------------
?? ??? ?
?? ?}
</script>2.接收數(shù)據(jù)的同時(shí)進(jìn)行 類型限制
<template>
?? ?<div>
?? ??? ?<div>子組件</div>
?? ??? ?<h2>學(xué)生姓名:{{ name }}</h2>
?? ??? ?<h2>年齡:{{ age }}</h2>
?? ?</div>
</template>
<script>
?? ?export default {
?? ??? ?name: 'Student',
?? ??? ?data() {
?? ??? ??? ?return {}
?? ??? ?},
?? ??? ?
?? ??? ?// 接收數(shù)據(jù)的同時(shí)進(jìn)行類型限制 ---
?? ??? ?props: {
?? ??? ??? ?name: String,
?? ??? ??? ?age: Number
?? ??? ?}
?? ??? ?// --------------------------
?? ??? ?
?? ?}
</script>3.接收數(shù)據(jù)的同時(shí)對(duì) 數(shù)據(jù)類型、必要性、默認(rèn)值 進(jìn)行限制
<template>
?? ?<div>
?? ??? ?<div>子組件</div>
?? ??? ?<h2>學(xué)生姓名:{{ name }}</h2>
?? ??? ?<h2>年齡:{{ age }}</h2>
?? ?</div>
</template>
<script>
?? ?export default {
?? ??? ?name: 'Student',
?? ??? ?data() {
?? ??? ??? ?return {}
?? ??? ?},
?? ??? ?// 接收數(shù)據(jù)的同時(shí)對(duì) 數(shù)據(jù)類型、必要性、默認(rèn)值 進(jìn)行限制 -----
?? ??? ?props: {
?? ??? ??? ?name: {
?? ??? ??? ??? ?type: String,?? ? ?// name傳入類型必須為字符串
?? ??? ??? ??? ?required: true ? ?// name設(shè)為必傳字段
?? ??? ??? ?},
?? ??? ??? ?age: {
?? ??? ??? ??? ?type: Number,
?? ??? ??? ??? ?default: 233 ? ? ?// 默認(rèn)值
?? ??? ??? ?}
?? ??? ?},
?? ??? ?// ------------------------------------------------
?? ?}
</script>到此這篇關(guān)于分享Vue子組件接收父組件傳值的3種方式的文章就介紹到這了,更多相關(guān)Vue子組件接收父組件傳值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue中組件(Component)和插件(Plugin)的區(qū)別及說明
這篇文章主要介紹了Vue中組件(Component)和插件(Plugin)的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
關(guān)于iview按需引用后使用this.$Modal報(bào)錯(cuò)的解決
這篇文章主要介紹了關(guān)于iview按需引用后使用this.$Modal報(bào)錯(cuò)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Vue3項(xiàng)目中配置TypeScript和JavaScript的兼容
在Vue3開發(fā)中,常見的使用JavaScript(JS)編寫代碼,但也會(huì)有調(diào)整編寫語言使用TypeScript(TS)的需求,因此,在Vue3項(xiàng)目設(shè)置中兼容TS和JS是刻不容緩的重要任務(wù),2023-08-08
解決iView中時(shí)間控件選擇的時(shí)間總是少一天的問題
下面小編就為大家分享一篇解決iView中時(shí)間控件選擇的時(shí)間總是少一天的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03
rollup3.x+vue2打包組件的實(shí)現(xiàn)
本文主要介紹了rollup3.x+vue2打包組件的實(shí)現(xiàn),詳細(xì)的介紹了打包會(huì)存在的問題,包版本的問題,babel 轉(zhuǎn)換jsx等問題,具有一定的參考價(jià)值,感興趣的可以了解一下2023-03-03
vant/vue手機(jī)端長(zhǎng)按事件以及禁止長(zhǎng)按彈出菜單實(shí)現(xiàn)方法詳解
這篇文章主要介紹了vant/vue手機(jī)端長(zhǎng)按事件以及禁止長(zhǎng)按彈出菜單實(shí)現(xiàn)方法詳解,需要的朋友可以參考下2022-12-12
Vue3時(shí)間軸組件問題記錄(時(shí)間信息收集組件)
本文介紹了如何在Vue3項(xiàng)目中封裝一個(gè)時(shí)間信息收集組件,采用雙向綁定響應(yīng)式數(shù)據(jù),通過對(duì)Element-Plus的Slider組件二次封裝,實(shí)現(xiàn)時(shí)間軸功能,解決了小數(shù)計(jì)算導(dǎo)致匹配問題和v-model綁定組件無效問題,感興趣的朋友跟隨小編一起看看吧2024-09-09

