本篇文章主要探討vue制作自定義按鈕的方法的解決方法。有一定的參考價(jià)值,有需要的朋友可以參考一下,跟隨小編一起來看解決方法吧。

在實(shí)際開發(fā)項(xiàng)目中,有時(shí)我們會(huì)用到自定義按鈕;因?yàn)橐粋€(gè)項(xiàng)目中,眾多的頁面,為了統(tǒng)一風(fēng)格,我們會(huì)重復(fù)用到很多相同或相似的按鈕,這時(shí)候,自定義按鈕組件就派上了大用場(chǎng),我們把定義好的按鈕組件導(dǎo)出,在全局引用,就可以在其他組件隨意使用啦,這樣可以大幅度的提高我們的工作效率。
好了,話不多說,上代碼:
img-button.vue//這是我們自定義按鈕組件
<template>
<div>
<!-- 圖片按鈕 -->
<div v-if="type === '查看'" :title="tag === '' ? type : tag" class="img-btn check-img"></div>
<div v-if="type === '添加'" :title="tag === '' ? type : tag" class="img-btn add-img"></div>
<div v-if="type === '修改'" :title="tag === '' ? type : tag" class="img-btn edit-img"></div>
<div v-if="type === '刪除'" :title="tag === '' ? type : tag" class="img-btn delete-img"></div>
<div v-if="type === '刷新'" :title="tag === '' ? type : tag" class="img-btn refresh-img"></div>
<div v-if="type === '關(guān)閉'" :title="tag === '' ? type : tag" class="img-btn close-img"></div>
<div v-if="type === '齒輪'" :title="tag === '' ? type : tag" class="img-btn gear-img"></div>
<div v-if="type === '平面圖'" :title="tag === '' ? type : tag" class="img-btn plan-img"></div>
<div v-if="type === '地圖'" :title="tag === '' ? type : tag" class="img-btn map-img"></div>
<div v-if="type === '一般模板'" :title="tag === '' ? type : tag" class="img-btn normal-img"></div>
<div v-if="type === '特殊模板'" :title="tag === '' ? type : tag" class="img-btn special-img"></div>
<div v-if="type === '折線圖'" :title="tag === '' ? type : tag" class="img-btn line-img"></div>
<!-- 文字按鈕 自定義大小-->
<div v-if="type === '按鈕'" :title="tag === '' ? name : tag" class="img-btn ibtn">{{name}}</div>
<div v-if="type === '小按鈕'" :title="tag === '' ? name : tag">{{name}}</div>
<div v-if="type === '普通按鈕'" :title="tag === '' ? name : tag">{{name}}</div>
</div>
</template>
<script>
export default {
name: 'ImgButton',
props: {
type: {
type: String,
default: ''
},
name: {
type: String,
default: ''
},
tag: {
type: String,
default: ''
}
}
}
</script>
<style scoped>
.img-button {
vertical-align: middle;
display: inline-block;
cursor: pointer;
margin-right: 10px;
.img-btn {
.img-no-repeat;
width:25px;
height:25px;
}
.img-btn:hover {
transform: scale(1.1);
}
.refresh-img {
background-image: url('../../assets/images/button/refresh.png');
}
.add-img {
background-image: url('../../assets/images/button/add.png');
}
.delete-img {
background-image: url('../../assets/images/button/delete.png');
}
.check-img {
background-image: url('../../assets/images/button/check.png');
}
.close-img {
background-image: url('../../assets/images/button/close.png');
}
.edit-img {
background-image: url('../../assets/images/button/edit.png');
}
.gear-img {
background-image: url('../../assets/images/button/gear.png')
}
.plan-img {
background-image: url('../../assets/images/button/plan.png')
}
.map-img {
background-image: url('../../assets/images/button/map.png')
}
.normal-img {
background-image: url('../../assets/images/button/normal.png')
}
.special-img {
background-image: url('../../assets/images/button/special.png')
}
.line-img {
background-image: url('../../assets/images/button/line_chart.png')
}
.ibtn {
width: auto;
min-width: 100px;
padding: 0 20px;
font-size: 17px;
height: 30px;
line-height: 30px;
text-align: center;
border-radius:15px;
background-color: #2f5d98;
vertical-align: middle;
color:#00cccc;
}
.ibtn-samll {
.ibtn;
height: 25px;
padding: 0 2px;
font-size: 10px;
line-height: 25px;
border-radius: 0px;
background-color: transparent;
border: 1px solid #00cccc;
}
.ibtn-samll:hover {
color: white;
border: 1px solid white;
}
.normal-btn {
.ibtn;
}
.normal-btn:hover {
color: white;
background-color: #ff9247;
}
}
</style>在router.js中配置好路由
在main.js中引入
//注冊(cè)自定義按鈕 import imgButton from './components/img-button' Vue.use(imgButton)
然后就可以在其他組件使用了
<imgButton type='刷新' @click.native='refreshBtn'></imgButton>
//值得注意的是 自定義按鈕組件添加點(diǎn)擊事件的時(shí)候一定要加.native 因?yàn)?native 修飾符就是用來注冊(cè)元素的原生事件而不是組件自定義事件的
以上就是vue制作自定義按鈕的方法的具體操作,代碼詳細(xì)清楚,如果在日常工作遇到這個(gè)問題,希望你能通過這篇文章解決問題。如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前題目:vue制作自定義按鈕的方法-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://chinadenli.net/article4/cepeie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、做網(wǎng)站、網(wǎng)站策劃、手機(jī)網(wǎng)站建設(shè)、軟件開發(fā)、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容