最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue子組件向父組件傳值示范方法

 更新時(shí)間:2023年03月20日 11:14:39   作者:H_HX126  
這篇文章主要介紹了Vue子組件向父組件傳值方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧

一、要點(diǎn)概述

子組件:通過某種事件(這里是@click點(diǎn)擊事件,也可以是其他事件)發(fā)送數(shù)據(jù),this.$emit('事件名',要傳的數(shù)據(jù))

父組件:在標(biāo)簽內(nèi)部@子組件中定義的事件名,等于一個(gè)函數(shù)(這里是rev),通過rev(val)這個(gè)函數(shù)接收數(shù)據(jù),把val賦值給自己的數(shù)據(jù)

二、分步講解

初始化Vue實(shí)例,可以理解為父組件,在父組件中的data中初始化一個(gè)變量(parentmsg),用來接收值;

let vm = new Vue({
    el: '#app',
    data: {
        parentmsg:''
    }
});

自定義子組件,命名為Child,這個(gè)名字可以隨意起,template里直接給一個(gè)id名,可以直接在html中寫組件的內(nèi)容,不再需要使用模板字符串寫模板了,既方便又快捷;

在子組件的data函數(shù)里聲明一個(gè)變量(childmsg);

在子組件中寫一個(gè)點(diǎn)擊事件@click="send()",send函數(shù)內(nèi)部通過this.$emitthis.$emit('childsend',this.childmsg)向父組件發(fā)送數(shù)據(jù);this.$emit的第一個(gè)參數(shù)為事件名,自定義的,父組件需要通過這個(gè)事件名接收值;第二個(gè)參數(shù)為要傳給父組件的數(shù)據(jù);

Vue.component('Child',{
    template:'#tp',
    data() {
        return {
            childmsg:'這是子組件中的數(shù)據(jù)'
        }
    },
    methods: {
        send() {
            // 第一個(gè)參數(shù)為事件名,自定義的,父組件需要通過這個(gè)事件名接收值
            // 第二個(gè)參數(shù)為要傳給父組件的數(shù)據(jù)
            this.$emit('childsend',this.childmsg)
        }
    }
})
<!-- 子組件模板內(nèi)容 -->
<template id="tp">
    <div>
        <button @click="send">點(diǎn)我向父組件傳值</button>
    </div>
</template>

在父組件中,通過@子組件中定義的事件名,觸發(fā)一個(gè)函數(shù)rev(val)來接收數(shù)據(jù),把接收到的val值賦給自己的變量parentmsg,然后就可以在html中使用插值表達(dá)式或v-bind綁定屬性值來使用子組件發(fā)送的數(shù)據(jù)了。

<div id="app">
    <Child @childsend="rev"></Child>
    <h3>{{parentmsg}}</h3>
</div>
methods: {
    // 父組件接收數(shù)據(jù)的函數(shù)
    rev(val) {
        // val就是子組件發(fā)送的數(shù)據(jù)
        this.parentmsg = val
    }
}

三、總代碼和運(yùn)行結(jié)果

<!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>子向父?jìng)髦?lt;/title>
</head>
<body>
     <!-- 父組件 @childsend="rev" -->
    <div id="app">
        <Child @childsend="rev"></Child>
        <h3>{{parentmsg}}</h3>
    </div>
    <!-- 子組件 this.$emit('childsend',this.childmsg) -->
    <template id="tp">
        <div>
            <button @click="send">點(diǎn)我向父組件傳值</button>
        </div>
    </template>
    <script src='https://cdn.jsdelivr.net/npm/vue/dist/vue.js'></script>
    <script>
        // 自定義子組件
        Vue.component('Child',{
            template:'#tp',
            data() {
                return {
                    childmsg:'這是子組件中的數(shù)據(jù)'
                }
            },
            methods: {
                send() {
                    // 第一個(gè)參數(shù)為事件名,自定義的,父組件需要通過這個(gè)事件名接收值
                    // 第二個(gè)參數(shù)為要傳給父組件的數(shù)據(jù)
                    this.$emit('childsend',this.childmsg)
                }
            }
        })
        let vm = new Vue({
            el: '#app',
            data: {
                parentmsg:''
            },
            methods: {
                // 父組件接收數(shù)據(jù)的函數(shù)
                rev(val) {
                    // val就是子組件發(fā)送的數(shù)據(jù)
                    this.parentmsg = val
                }
            }
        });
    </script>
</body>
</html>

點(diǎn)擊之后父組件才能訪問子組件中的數(shù)據(jù)

到此這篇關(guān)于Vue子組件向父組件傳值示范方法的文章就介紹到這了,更多相關(guān)Vue子向父?jìng)髦祪?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

大埔区| 获嘉县| 湖口县| 兖州市| 刚察县| 南昌县| 日照市| 蒙阴县| 西乌珠穆沁旗| 肇州县| 舒城县| 穆棱市| 深泽县| 图木舒克市| 定襄县| 天柱县| 双桥区| 鄢陵县| 宣化县| 工布江达县| 翁牛特旗| 蓝田县| 张家港市| 柯坪县| 漳平市| 陈巴尔虎旗| 阿巴嘎旗| 筠连县| 广灵县| 道孚县| 宝坻区| 尉氏县| 黄龙县| 茌平县| 合水县| 平湖市| 冷水江市| 新和县| 四平市| 沙坪坝区| 二连浩特市|