博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vue 利用自定义绑定事件来 传值 实现子传父数据,父传子数据
阅读量:5962 次
发布时间:2019-06-19

本文共 1453 字,大约阅读时间需要 4 分钟。

例子

用$emit  绑定自定义事件 $emit("receive",item) 然后再触发父子的自定义事件 执行函数  v-on:receive="changeValueHandle"    changeValueHandle(value){this.val = value;}  item 就是要传入父级的值,value就是接收 item的

<body>
        <div id="app">
            <div style="float: left;">
                <h2>自定义的下拉框</h2>
                <custom-select btn="查询" v-bind:list="list1"></custom-select>
            </div>
            <div style="float:left;">
                <h2>自定义的下拉框2</h2>
                <custom-select btn="搜索" v-bind:list="list2"></custom-select>
            </div>
        </div>
        <script>
        /*var obj = {
                    selectShow:false
                }*/
            //注册组件
            Vue.component("custom-select",{
                data:function(){
                    return {
                        selectShow:false,
                        val:""
                    };
                },
                props:["btn","list"],
                template:`<section class="warp">
                    <div class="searchIpt clearFix">
                        <div class="clearFix">
                            <input type="text" class="keyWord" :value="val" @click="selectShow = !selectShow" />
                            <input type="button" :value="btn">
                            <span></span>
                        </div>
                        <custom-list
                            v-show="selectShow"
                            :list="list"
                            v-on:receive="changeValueHandle"
                        ></custom-list>
                    </div>
                </section>`,
                methods:{
                    changeValueHandle(value){
                        //alert("我被触发了,值为:"+value);
                        this.val = value;
                    }
                }
            })
            Vue.component("custom-list",{
                props:["list"],
                template:`<ul class="list">
                            <li v-for="item of list" @click="selectValueHandle(item)">{
{item}}</li>
                        </ul>`,
                methods:{
                    selectValueHandle:function(item){
                        //在子组件中有交互
                        //告知父级,改变val的值,需要出发一个自定义事件
                        this.$emit("receive",item);
                    }
                }
            })      
            new Vue({
                el:"#app",
                data:{
                    list1:["北京","上海","杭州"],
                    list2:["17-02-17","17-02-18","17-02-19"]
                }
            });
        </script>
    </body>

转载于:https://www.cnblogs.com/blccy/p/8215130.html

你可能感兴趣的文章
Git Flow
查看>>
Objective-C --- - UICollectionView (梳理总结)
查看>>
我的友情链接
查看>>
jsoup将外部样式修改为内嵌样式
查看>>
存储方案与存储产品之NAS篇
查看>>
鸟哥学习笔记---网络基本管理
查看>>
鸟哥学习笔记---SAMBA
查看>>
JSON.parse()和JSON.stringify()
查看>>
完整的WordPress函数大全
查看>>
Citrix xenapp记录
查看>>
2011,我的IT我的梦
查看>>
×××lamp
查看>>
LAMP+Postfix+Dovecot+Postfixadmin搭建邮件管理系统(七)
查看>>
MooseFS(MFS)安装手记(二)
查看>>
我的友情链接
查看>>
第二章Java语言基本语法————数据类型
查看>>
Linux下安装Sybase
查看>>
金蝶记账王用试算平衡表的教程
查看>>
如何从架构与测试推动敏捷开发_主頁
查看>>
adb操作命令
查看>>