vue.js封裝switch開(kāi)關(guān)組件的操作
我的項(xiàng)目本來(lái)用的element,但是switch開(kāi)關(guān)不符合設(shè)計(jì)要求,于是自己封裝了一個(gè)switch組件,并且實(shí)現(xiàn)了switch開(kāi)關(guān)的雙向數(shù)據(jù)綁定

<template>
<label role="checkbox" :class="['switch', { toggled }]">
<input type="checkbox" class="switch-input" @change="toggle" />
<div
class="switch-core"
:style="{ backgroundColor: toggled ? colorChecked : colorUnchecked }"
>
<div
class="switch-button"
:style="{
transition: `transform ${speed}ms`,
transform: toggled ? null : `translate3d(32px, 0px, 0px)`
}"
></div>
</div>
<span class="switch-label label-right" v-if="toggled" v-html="labelChecked">
</span>
<span class="switch-label label-left" v-html="labelUnchecked" v-else>
</span>
</label>
</template>
<script>
export default {
name: "ToggleSwitch",
data() {
return {
toggled: this.value
};
},
props: {
value: {
type: Boolean,
default: true
},
speed: {
type: Number,
default: 100
},
labelChecked: {
type: String,
default: "啟用"
},
labelUnchecked: {
type: String,
default: "停用"
},
colorChecked: {
type: String,
default: "#11CED2"
},
colorUnchecked: {
type: String,
default: "#E6EAF1"
}
},
watch: {
value: function(val) {
this.value = val;
}
},
methods: {
toggle(event) {
this.toggled = !this.toggled;
this.$emit("update:value", this.toggled);
this.$emit("change", event);
}
}
};
</script>
<style lang="scss" scoped>
.switch {
display: inline-block;
position: relative;
overflow: hidden;
vertical-align: middle;
user-select: none;
font-size: 10px;
cursor: pointer;
.switch-input {
display: none;
}
.switch-label {
position: absolute;
top: 0;
font-weight: 600;
color: white;
z-index: 2;
&.label-left {
left: 10px;
line-height: 20px;
border-top-left-radius: 2px;
border-bottom-left-radius: 2px;
color: #b5bdc8;
}
&.label-right {
right: 10px;
line-height: 20px;
border-top-right-radius: 2px;
border-bottom-right-radius: 2px;
}
}
.switch-core {
display: block;
position: relative;
box-sizing: border-box;
outline: 0;
margin: 0;
transition: border-color 0.3s, background-color 0.3s;
user-select: none;
width: 46px;
height: 20px;
border-radius: 4px;
line-height: 20px;
.switch-button {
width: 8px;
height: 16px;
display: block;
position: absolute;
overflow: hidden;
top: 2;
left: 2;
z-index: 3;
transform: translate3d(0, 0, 0);
background-color: rgba(255, 255, 255, 1);
border-radius: 4px;
margin-top: 2px;
margin-left: 2px;
}
}
}
</style>
調(diào)用開(kāi)關(guān)組件
<toggle-switch
v-model="value"
:value.sync="value"
@change="switchChange"
>
</toggle-switch>
組件實(shí)現(xiàn)了switch開(kāi)關(guān)的雙向數(shù)據(jù)綁定,在父組件的methods中寫(xiě)一個(gè)@change事件
switchChange() {
console.log("switch值改變");
},
補(bǔ)充知識(shí):vue 監(jiān)控el-switch控件值的變化
我要的效果是根據(jù)系統(tǒng)消息的推送范圍決定推送人標(biāo)簽的顯示,如下圖兩種情況:
——選擇全站推送

——選擇個(gè)人推送

——頁(yè)面定義的data對(duì)象

el-switch標(biāo)簽控件的代碼, v-model="entity.pushRange"綁定的是推送范圍字段
<el-form-item label="推送范圍:" :label-width="formLabelWidth" prop="pushRange">
<el-switch
v-model="entity.pushRange"
active-color="#13ce66"
inactive-color="#ff4949"
active-text="全站"
inactive-text="個(gè)人"
@change="parens2($event)">
</el-switch>
</el-form-item>
下面的推送人id文本框,v-if=“FalgpushId”用來(lái)控制該文本框的顯示,等于false時(shí)隱藏,true時(shí)顯示(默認(rèn)值為初始化時(shí)定義的FalgpushId = false,所以隱藏掉了)
<!-- 推送人id --> <el-form-item label="推送人ID:" :label-width="formLabelWidth" prop="pushId" v-if="FalgpushId"> <el-input v-model="entity.pushId" :disabled="disabled" clearable></el-input> </el-form-item>
methods中的方法,通過(guò)$event寫(xiě)法來(lái)監(jiān)控該控件值的變化
methods:{
//該方法傳入推送范圍值,根據(jù)判斷,決定是否展示其下面的推送人ID文本框
parens2(value){
let self = this ;
if(value == false){
//el-switch控件為 個(gè)人推送時(shí),value為false
self.FalgpushId = true; //推送人id文本框顯示
self.entity.pushRange = false;
}else if(value == true){
//el-switch控件為 true 全站推送,value為true
self.FalgpushId = false; //推送人id文本框隱藏
self.entity.pushRange = true;
}
},
}
以上這篇vue.js封裝switch開(kāi)關(guān)組件的操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- vue如何點(diǎn)擊多個(gè)tab標(biāo)簽打開(kāi)關(guān)閉多個(gè)頁(yè)面
- vue.js實(shí)現(xiàn)開(kāi)關(guān)(switch)組件實(shí)例代碼
- vue自定義開(kāi)關(guān)組件使用詳解
- vue實(shí)現(xiàn)移動(dòng)端的開(kāi)關(guān)按鈕
- Vue實(shí)現(xiàn)開(kāi)關(guān)按鈕拖拽效果
- vue自定義switch開(kāi)關(guān)組件,實(shí)現(xiàn)樣式可自行更改
- vue中el-tree結(jié)合el-switch實(shí)現(xiàn)開(kāi)關(guān)狀態(tài)切換
相關(guān)文章
vue中使用 pako.js 解密 gzip加密字符串的方法
這篇文章主要介紹了vue項(xiàng)目中 使用 pako.js 解密 gzip加密字符串 的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
Vue數(shù)據(jù)驅(qū)動(dòng)表單渲染,輕松搞定form表單
這篇文章主要介紹了Vue數(shù)據(jù)驅(qū)動(dòng)表單渲染,輕松搞定form表單,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Vue 使用beforeEach實(shí)現(xiàn)登錄狀態(tài)檢查功能
今天小編就為大家分享一篇Vue 使用beforeEach實(shí)現(xiàn)登錄狀態(tài)檢查功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
vue項(xiàng)目中定時(shí)器無(wú)法清除的原因解決
頁(yè)面有定時(shí)器,并且定時(shí)器在離開(kāi)頁(yè)面時(shí),有清除,本文主要介紹了vue項(xiàng)目中定時(shí)器無(wú)法清除的原因解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
vue-router相關(guān)基礎(chǔ)知識(shí)及工作原理
這篇文章主要介紹了vue-router相關(guān)基礎(chǔ)知識(shí)及單頁(yè)面應(yīng)用的工作原理,需要的朋友可以參考下2018-03-03

