vue中實現(xiàn)子組件接收父組件方法并獲取返回值
子組件接收父組件方法并獲取返回值
項目中有時候會遇到父子組件傳值的問題,比如子組件需要接收父組件方法并獲取該方法返回值的時候。
使用this.$emit('方法名', '參數(shù)1', '參數(shù)2')的方式,獲取到不是父組件方法的return值。但是我們可以將參數(shù)改為回調(diào)函數(shù)的形式,父組件里執(zhí)行該回調(diào)函數(shù),返回值后給子組件,子組件再接收返回值。
示例:
父組件 GetCallback.vue
<template> ? <div> ? ? 我是父組件 ? ? <CallbackChild1 @getData="getData" /> ? </div> </template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import CallbackChild1 from '@/components/CallbackChild1.vue';
@Component({
? components: {
? ? CallbackChild1,
? },
})
export default class GetCallback extends Vue {
? getData(callback: FunctionConstructor): void {
? ? const name = '我是父組件的值';
? ? callback(name); // 父組件執(zhí)行作為參數(shù)的函數(shù)
? }
}
</script>子組件 CallbackChild1.vue
<template>
? <div>
? ? <p>我是子組件</p>
? ? <button @click="getDataFromParent">子組件獲取父組件返回值</button>
? ? <span>返回值:{{ value }}</span>
? </div>
</template><script lang="ts">
import { Vue, Component } from 'vue-property-decorator';
@Component
export default class CallbackChild1 extends Vue {
? value = '';
? getDataFromParent(): string {
? ?? ?// 子組件接收函數(shù)的值
? ? this.$emit('getData', (val: string) => { this.value = val });
? ? return this.value;
? }
}
</script>子組件接收父組件的另一種方法
子組件獲取父組件傳遞的數(shù)據(jù)通常是通過props屬性接收父組件的傳遞過來的數(shù)據(jù),
代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div id="app"> <my-child :msg='msg'></my-child> </div> </body> </html>
<script src="./node_modules//vue//dist//vue.min.js"></script>
<script>
let vm = new Vue({
el: '#app',
data: {
msg: 'helloword'
},
components: {
'myChild': {
props: ['msg'],
mounted() {
console.log(this.$attrs)
},
components: {
'myChildren': {
props: ['msg'],
template:
`<span>{{msg}}</span>
`
}
},
template: `<div>{{msg}}
<my-children :msg='msg'></children>
</div>`
}
}
})
</script>也可以通過子組件的$attrs接收的父組件的數(shù)據(jù),但是這時候傳遞的數(shù)據(jù)子組件中不能有props的屬性,不然子組件的$attrs獲得的是空對象,
代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div id="app"> <my-child :msg='msg'></my-child> </div> </body> </html>
<script src="./node_modules//vue//dist//vue.min.js"></script>
<script>
let vm = new Vue({
el: '#app',
data: {
msg: 'helloword'
},
components: {
'myChild': {
// props:['msg'],
mounted() {
console.log(this.$attrs)
},
components: {
'myChildren': {
//props:['msg'],
template:
`<span>{{this.$attrs.msg}}</span>
`
}
},
template: `<div>{{this.$attrs.msg}}
<my-children :msg='$attrs.msg'></children>
</div>`
}
}
})
</script>以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- vue父組件中獲取子組件中的數(shù)據(jù)(實例講解)
- vue父組件異步獲取數(shù)據(jù)傳給子組件的方法
- 詳解VUE里子組件如何獲取父組件動態(tài)變化的值
- Vue父組件如何獲取子組件中的變量
- vue 父組件通過$refs獲取子組件的值和方法詳解
- vue.js 子組件無法獲取父組件store值的解決方式
- vue實現(xiàn)父組件獲取子組件的方法或?qū)傩灾翟斀?/a>
- vue子組件如何獲取父組件的內(nèi)容(props屬性)
- vue子組件獲取到它父組件數(shù)據(jù)的4種方法
- VUE父組件異步獲取數(shù)據(jù),子組件接收的值為空的問題
- vue父組件異步如何獲取數(shù)據(jù)傳給子組件
- vue3父組件使用ref獲取子組件的屬性和方法
- vue子組件實時獲取父組件的數(shù)據(jù)實現(xiàn)
相關(guān)文章
使用Vant如何實現(xiàn)數(shù)據(jù)分頁,下拉加載
這篇文章主要介紹了使用Vant實現(xiàn)數(shù)據(jù)分頁及下拉加載方式。具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
關(guān)于Vue.js一些問題和思考學(xué)習(xí)筆記(2)
這篇文章主要為大家分享了關(guān)于Vue.js一些問題和思考的學(xué)習(xí)筆記,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
vue react中的excel導(dǎo)入和導(dǎo)出功能
當(dāng)我們把信息化系統(tǒng)給用戶使用時,用戶經(jīng)常需要把以前在excel里錄入的數(shù)據(jù)導(dǎo)入的信息化系統(tǒng)里,這樣為用戶提供了很大的方便,這篇文章主要介紹了vue中或者react中的excel導(dǎo)入和導(dǎo)出,需要的朋友可以參考下2023-09-09
Vue使用v-model封裝el-pagination組件的全過程
通過封裝el-pagination組件開發(fā)自定義分頁組件的類似文章網(wǎng)上已經(jīng)有很多了,但看了一圈,總是不如意,于是決定還是自己動手搞一個,對v-model封裝el-pagination組件相關(guān)知識感興趣的朋友一起看看吧2021-07-07

