亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue中實(shí)現(xiàn)子組件接收父組件方法并獲取返回值

 更新時間:2022年08月04日 09:11:15   作者:風(fēng)如也  
這篇文章主要介紹了vue中實(shí)現(xiàn)子組件接收父組件方法并獲取返回值方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

子組件接收父組件方法并獲取返回值

項(xiàng)目中有時候會遇到父子組件傳值的問題,比如子組件需要接收父組件方法并獲取該方法返回值的時候。

使用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)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue實(shí)現(xiàn)循環(huán)切換動畫

    vue實(shí)現(xiàn)循環(huán)切換動畫

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)循環(huán)切換動畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • 使用Vant如何實(shí)現(xiàn)數(shù)據(jù)分頁,下拉加載

    使用Vant如何實(shí)現(xiàn)數(shù)據(jù)分頁,下拉加載

    這篇文章主要介紹了使用Vant實(shí)現(xiàn)數(shù)據(jù)分頁及下拉加載方式。具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 關(guān)于Vue.js一些問題和思考學(xué)習(xí)筆記(2)

    關(guān)于Vue.js一些問題和思考學(xué)習(xí)筆記(2)

    這篇文章主要為大家分享了關(guān)于Vue.js一些問題和思考的學(xué)習(xí)筆記,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • vue中用動態(tài)組件實(shí)現(xiàn)選項(xiàng)卡切換效果

    vue中用動態(tài)組件實(shí)現(xiàn)選項(xiàng)卡切換效果

    本篇文章主要介紹了vue中用動態(tài)組件實(shí)現(xiàn)選項(xiàng)卡切換效果,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Vue3和Vite不得不說的那些事

    Vue3和Vite不得不說的那些事

    這篇文章主要為大家詳細(xì)介紹了Vue3和Vite的那些事,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • VUE+Canvas實(shí)現(xiàn)簡單五子棋游戲的全過程

    VUE+Canvas實(shí)現(xiàn)簡單五子棋游戲的全過程

    這篇文章主要給大家介紹了關(guān)于VUE+Canvas實(shí)現(xiàn)簡單五子棋游戲的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • Vue如何實(shí)現(xiàn)自動觸發(fā)功能

    Vue如何實(shí)現(xiàn)自動觸發(fā)功能

    這篇文章主要介紹了Vue如何實(shí)現(xiàn)自動觸發(fā)功能,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • vue react中的excel導(dǎo)入和導(dǎo)出功能

    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微信分享出來的鏈接點(diǎn)開是首頁問題的解決方法

    vue微信分享出來的鏈接點(diǎn)開是首頁問題的解決方法

    這篇文章主要為大家詳細(xì)介紹了vue微信分享出來的鏈接點(diǎn)開是首頁問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Vue使用v-model封裝el-pagination組件的全過程

    Vue使用v-model封裝el-pagination組件的全過程

    通過封裝el-pagination組件開發(fā)自定義分頁組件的類似文章網(wǎng)上已經(jīng)有很多了,但看了一圈,總是不如意,于是決定還是自己動手搞一個,對v-model封裝el-pagination組件相關(guān)知識感興趣的朋友一起看看吧
    2021-07-07

最新評論