vue中實(shí)現(xiàn)子組件接收父組件方法并獲取返回值
子組件接收父組件方法并獲取返回值
項(xiàng)目中有時(shí)候會(huì)遇到父子組件傳值的問(wèn)題,比如子組件需要接收父組件方法并獲取該方法返回值的時(shí)候。
使用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ù)通常是通過(guò)props屬性接收父組件的傳遞過(guò)來(lái)的數(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>也可以通過(guò)子組件的$attrs接收的父組件的數(shù)據(jù),但是這時(shí)候傳遞的數(shù)據(jù)子組件中不能有props的屬性,不然子組件的$attrs獲得的是空對(duì)象,
代碼如下:
<!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>以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- vue父組件中獲取子組件中的數(shù)據(jù)(實(shí)例講解)
- vue父組件異步獲取數(shù)據(jù)傳給子組件的方法
- 詳解VUE里子組件如何獲取父組件動(dòng)態(tài)變化的值
- Vue父組件如何獲取子組件中的變量
- vue 父組件通過(guò)$refs獲取子組件的值和方法詳解
- vue.js 子組件無(wú)法獲取父組件store值的解決方式
- vue實(shí)現(xiàn)父組件獲取子組件的方法或?qū)傩灾翟斀?/a>
- vue子組件如何獲取父組件的內(nèi)容(props屬性)
- vue子組件獲取到它父組件數(shù)據(jù)的4種方法
- VUE父組件異步獲取數(shù)據(jù),子組件接收的值為空的問(wèn)題
- vue父組件異步如何獲取數(shù)據(jù)傳給子組件
- vue3父組件使用ref獲取子組件的屬性和方法
- vue子組件實(shí)時(shí)獲取父組件的數(shù)據(jù)實(shí)現(xiàn)
相關(guān)文章
Vue Element前端應(yīng)用開(kāi)發(fā)之樹(shù)列表組件
本篇隨筆主要介紹樹(shù)列表組件和下拉列表樹(shù)組件在項(xiàng)目中的使用,以及一個(gè)SplitPanel的組件。2021-05-05
關(guān)于實(shí)現(xiàn)Vue3版抖音滑動(dòng)插件踩坑指南
這篇文章主要給大家介紹了關(guān)于實(shí)現(xiàn)Vue3版抖音滑動(dòng)插件踩坑指南,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue3具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02
Vue在項(xiàng)目中如何引入本地Json數(shù)據(jù)
這篇文章主要介紹了Vue在項(xiàng)目中如何引入本地Json數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
詳解vue中使用axios對(duì)同一個(gè)接口連續(xù)請(qǐng)求導(dǎo)致返回?cái)?shù)據(jù)混亂的問(wèn)題
這篇文章主要介紹了詳解vue中使用axios對(duì)同一個(gè)接口連續(xù)請(qǐng)求導(dǎo)致返回?cái)?shù)據(jù)混亂的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

