Vue.js仿Select下拉框效果

废话少说,直接上图上代码:

效果图:

Vue.js仿Select下拉框效果

HEML:

<div> <h2>自定义下拉框</h2> <imitate-select h2-value="第一个 Select" v-bind:list="list1"></imitate-select> <h2>自定义下拉框</h2> <imitate-select h2-value="第二个 Select" v-bind:list="list2"></imitate-select> </div>

Javascript:

<script> // 注册组件【 全局 】 Vue.component('imitate-select', { data: function(){ // 给每一个添加一个自己的对象 return { selectShoe: false, val: '' } }, props: ['h2Value', 'list'], // 这里的名称一定是驼峰式【如:上面是 h2-value(上面可以是烤串或驼峰式) ,下面必须是 h2Value】 template: `<section> <div> <h2>{{ h2Value }}</h2> <div> <input type="text" placeholder="模仿Select下拉框" @click="selectShoe = !selectShoe" :value="val" ><i></i> </div> <select-list v-show="selectShoe" :list="list" @receive="changeHandle"></select-list> </div> </section>`, methods: { changeHandle(value){ // 自定义事件 // alert('我被触发了!值为:' + value) this.val = value } } }); Vue.component('select-list', { props: ['list'], template: `<ul> <li v-for="item of list" @click="selectHandle(item)">{{ item }}</li> </ul>`, methods: { selectHandle: function (item){ this.$emit('receive', item) } } }); new Vue({ el: '#demo', data: { list1: ['HTML','CSS','Javascript','Vue.js'], list2: ['PHP','JAVA','C++','.NET'] } }) </script>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/cd5cb0b2f23cb85df8f3d119fc164a49.html