Vue子父組件之間傳值的三種方法示例
父組件向子組件傳值: props-父組件給子組件傳輸數(shù)據(jù)和驗證
1.父組件的代碼示例
<template>
<div>父組件</div>
// 引用子組件
<Dialog :fatherData="fatherData"></Dialog>
</template>
<script>
// 導(dǎo)入子組件
import Dialog from '@/components/Dialog.vue'
export default {
name: 'HomeView',
components: { Dialog },
data () {
return {
fatherData: '父組件的值',
}
}
}
</script>
2.子組件的代碼示例
<template>
<div>
<div>子組件</div>
<div>
//展示父組件數(shù)據(jù)
{{fatherData}}
</div>
</div>
</template>
<script>
export default {
data () {
return {
childrenData: '子組件自己的數(shù)據(jù)'
}
},
//props的基本用法是父組件給子組件傳輸數(shù)據(jù)和驗證
props: {
//父組件數(shù)據(jù)
fatherData: {
type: String
}
}
}
</script>
子組件向父組件傳值:this.$emit()-子組件可以使用 $emit,讓父組件監(jiān)聽到自定義事件
1.父組件的代碼示例
<template>
<div>父組件</div>
// 引用子組件
<Dialog @tranferData="tranferData"></Dialog>
//展示子組件的值
<div>{{ receiveData }}</div>
</template>
<script>
// 導(dǎo)入子組件
import Dialog from '@/components/Dialog.vue'
export default {
name: 'HomeView',
components: { Dialog },
data () {
return {
fatherData: '父組件的值',
receiveData:''
}
},
methods: {
//接收子組件傳過來的數(shù)據(jù)
tranferData(val){
console.log('子組件向父組件傳過來的值:',val)
this.receiveData = val
}
}
}
</script>
2.子組件的代碼示例
<template>
<div>
<div>子組件</div>
<el-button @click="tranfer">子向父傳值</el-button>
</div>
</template>
<script>
export default {
data () {
return {
childrenData: '子組件的值'
}
}
},
methods:{
tranfer(){
this.$emit('tranferData',this.childrenData)
}
}
}
</script>
通過refs調(diào)用子組件的值−用this.refs獲取到的是組件實例,可以使用組件的所有方法
1.父組件的代碼示例
<template>
<div>父組件</div>
// 引用子組件
<Dialog ref='dialogData'></Dialog>
<div>通過refs拿到子組件的值</div>
<el-button @click="toGet">refs拿到子組件的值</el-button>
</template>
<script>
// 導(dǎo)入子組件
import Dialog from '@/components/Dialog.vue'
export default {
name: 'HomeView',
components: { Dialog },
data () {
return {
fatherData: '父組件的值',
}
},
methods:{
toGet(){
// 通過refs調(diào)用子組件的值(childrenData)
const data = this.$refs.dialogData.childrenData
alert(data)
// 通過refs調(diào)用子組件的值(childrenWay())
const way = this.$refs.dialogData.childrenWay()
alert(way)
}
}
}
</script>
2.子組件的代碼示例
<template>
<div>
<div>子組件</div>
</div>
</template>
<script>
export default {
data () {
return {
childrenData: '子組件自己的數(shù)據(jù)'
}
},
methods:{
childrenWay(){
alert('父組件調(diào)用子組件的方法')
}
}
}
</script>父組件使用子組件傳過來的值
如下面的代碼所示,closeData和confirmData分別接收取消和確定時子組件傳過來的值

總結(jié)
到此這篇關(guān)于Vue子父組件之間傳值的三種方法的文章就介紹到這了,更多相關(guān)Vue子父組件傳值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談vue在html中出現(xiàn){{}}的原因及解決方式
這篇文章主要介紹了淺談vue在html中出現(xiàn){{}}的原因及解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Vue使用v-for數(shù)據(jù)渲染順序混亂的原因及解決方案
在 Vue.js 中,使用 v-for 指令進(jìn)行數(shù)據(jù)渲染時,有時會遇到渲染順序混亂的問題,這種問題主要與 Vue 的響應(yīng)式系統(tǒng)、DOM 更新機制以及數(shù)組的變更方法有關(guān),以下是對這一現(xiàn)象的深入分析及解決方案,需要的朋友可以參考下2025-01-01
vue項目根據(jù)不同環(huán)境進(jìn)行設(shè)置打包命令的方法
這篇文章主要介紹了vue項目根據(jù)不同環(huán)境進(jìn)行設(shè)置打包命令,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-11-11
Vue項目打包壓縮的實現(xiàn)(讓頁面更快響應(yīng))
這篇文章主要介紹了Vue項目打包壓縮的實現(xiàn)(讓頁面更快響應(yīng)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
vue-router動態(tài)設(shè)置頁面title的實例講解
今天小編就為大家分享一篇vue-router動態(tài)設(shè)置頁面title的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
如何在vue3中使用滑塊檢驗vue-puzzle-verification
這篇文章主要介紹了在vue3中使用滑塊檢驗vue-puzzle-verification的相關(guān)資料,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11

