vue2 父組件向子組件傳遞函數


 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>評論列表實現</title>
</head>
<body>
<div id="app">
    <com2 @subshow="show"></com2>
</div>

<template id="tmp1">
    <div>
        <h1>這是子組件</h1>
        <input type="button" value="按我" @click="myclick">
    </div>
</template>
<script src="./lib/vue-2.4.0.js"></script>
<script>
    var com2={
        template:'#tmp1',

        data() {
            return {
                sonmsg: { name: '小頭兒子', age: 6 },
            }
        },
        methods:{
            myclick(){
                // this.$emit('parentShow')
                this.$emit('subshow',this.sonmsg)
            }
        }


    };
    // 創建 Vue 實例,得到 ViewModel
    var vm=new Vue({
        el:'#app',
        data:{},
        methods:{
            show(msg){
                console.log("這是父組件的方法");
                console.log(msg)
            }
        },
        components:{
            com2:com2
        }
    })

</script>
</body>
</html>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM