vuejs實現(xiàn)下拉框菜單選擇
本文實例為大家分享了vuejs實現(xiàn)下拉框菜單選擇的具體代碼,供大家參考,具體內(nèi)容如下
方法一:
<script type="text/ecmascript-6">
export default {
data() {
return {
isShowSelect: false,
dataList: [
{key: -1, value: "請選擇"},
{key: 0, value: "蘋果"},
{key: 1, value: "香蕉"}
]
unitName:'請選擇'
}
},
methods: {
arrowDown() {
this.isShowSelect = !this.isShowSelect;
},
select(item, index) {
this.isShowSelect = false;
console.log(item);
console.log(index);
this.unitModel = index;
this.unitName = item.value;
}
}
};
</script>
<li>
<h3 class="F7">下拉框選擇案例</h3>
<div class="por">
<div class="selectBox" style="width: 400px;">
<div class="selectBox_show" v-on:click.stop="arrowDown">
<i class="icon icon_arrowDown"></i>
<p title="請選擇">{{unitName}}</p>
<input type="hidden" name="unit" v-model="unitModel">
</div>
<div class="selectBox_list" v-show="isShowSelect"
style="max-height: 240px; width: 398px; display: block;">
<div class="selectBox_listLi" v-for="(item, index) in dataList"
@click.stop="select(item, index)">{{item.value}}
</div>
</div>
</div>
</div>
</li>
方法二:由父組件傳遞數(shù)據(jù)給子組件
<template>
<div class="selection-component">
<div class="selection-show" @click="toggleDrop">
<span>{{ selections[nowIndex].label }}</span>
<div class="arrow"></div>
</div>
<div class="selection-list" v-if="isDrop">
<ul>
<li v-for="(item, index) in selections" @click="chooseSelection(index)">{{ item.label }}</li>
</ul>
</div>
</div>
</template>
<script>
export default {
props: {
selections: {
type: Array,
default: [{
label: 'test',
value: 0
}]
}
},
data () {
return {
isDrop: false,
nowIndex: 0
}
},
methods: {
toggleDrop () {
this.isDrop = !this.isDrop
},
chooseSelection (index) {
this.nowIndex = index
this.isDrop = false
this.$emit('on-change', this.selections[this.nowIndex])
}
}
}
</script>

關(guān)于vue.js組件的教程,請大家點擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
更多vue學(xué)習(xí)教程請閱讀專題《vue實戰(zhàn)教程》
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中通過屬性綁定為元素綁定style行內(nèi)樣式的實例代碼
這篇文章主要介紹了Vue中通過屬性綁定為元素綁定style行內(nèi)樣式,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
vue ssr+koa2構(gòu)建服務(wù)端渲染的示例代碼
這篇文章主要介紹了vue ssr+koa2構(gòu)建服務(wù)端渲染的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
vue 使用 v-model 雙向綁定父子組件的值遇見的問題及解決方案
這篇文章主要介紹了vue 使用 v-model 雙向綁定父子組件的值遇見的問題及解決方案,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-03-03
從0到1構(gòu)建vueSSR項目之node以及vue-cli3的配置
這篇文章主要介紹了從0到1構(gòu)建vueSSR項目之node以及vue-cli3的配置,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03
vue+px2rem實現(xiàn)pc端大屏自適應(yīng)的實例代碼(rem適配)
不管是移動端的適配,還是大屏需求,都離不開不一個單位rem,rem是相對于根元素的字體大小的單位,下面這篇文章主要給大家介紹了關(guān)于vue+px2rem實現(xiàn)pc端大屏自適應(yīng)的相關(guān)資料,需要的朋友可以參考下2021-08-08

