美文网首页
组件emit使用

组件emit使用

作者: 程序猿的小生活 | 来源:发表于2022-06-16 09:29 被阅读0次
<html>
    <head>
         <script src="https://unpkg.com/vue@next"></script>
        <meta charset="utf-8">
        <title>组件$emit使用</title>
    </head>
    <body>
    <div id="app">
        <createcomponent @go="sondata"></createcomponent>
        {{fatherdata}}
    </div>  
    </body>
    <script>
        Vue.createApp({
            data(){
                return{
                    "fatherdata":"这是父亲数据"
                }
            },
            methods: {
            sondata(msg){
                this.fatherdata = msg;
            }
            },
            
            
        })
        .component("createcomponent",{
            data(){
                return{
                    msg:"这是儿子组件的数据"
                    
                }
            },
            "template":`<div @click="send">发送组件数据到父级<div>
            <div @click="$emit('go',this.msg)">第二种方式通过@click传值</div>`,
            methods: {
                send() {
                    this.$emit('go',this.msg);//go相当于自定义组件的自定义事件名称 msg表示传过去参数
                }
            },
            
        })
        .mount("#app")
        
        
    </script>
</html>

相关文章

网友评论

      本文标题:组件emit使用

      本文链接:https://www.haomeiwen.com/subject/vxofvrtx.html