vue自定義開(kāi)關(guān)組件使用詳解
本文實(shí)例為大家分享了vue自定義開(kāi)關(guān)組件的具體代碼,供大家參考,具體內(nèi)容如下
switch.vue:
<template> ? <div class="disLB"> ? ? <div class="switch disLB" @click="toggleSwitch" :class="isOpen?'switch-on':''"> ? ? ? <span class="disB switch-circle" :class="isOpen?'on':''"></span> ? ? </div> ? ?? ? </div> </template> <script> export default { ? data() { ? ? return { ? ? ? // isOpen: false ? ? } ? }, ? props: ["isOpen"], ? methods: { ? ? toggleSwitch() { ? ? ? // 子組件不能直接修改父組件的數(shù)據(jù),要通過(guò)$emit ? ? ? this.$emit('changeSwitch') ? ? } ? } ?? } </script> <style lang="less" scoped> ? .switch { ? ? position: relative; ? ? border-radius: 20px; ? ? border: 1px solid #dfdfdf; ? ? width: 45px; ? ? height: 23px; ? ? .switch-circle { ? ? ? position: absolute; ? ? ? width: 21px; ? ? ? height: 21px; ? ? ? background-color: #fff; ? ? ? border-radius: 50%; ? ? ? border: 1px solid #dfdfdf; ? ? ? box-shadow: 0 1px 1px #ccc; ? ? } ? ? .on { ? ? ? right: 0; ? ? ? // background-color: #64bd63; ? ? ? border-color: #64bd63; ? ? ? transform: translate(X); ? ? ? transition: transform 0.5s, right 0.5s; ? ? } ? } ? .switch-on { ? ? background-color: #64bd63; ? } </style>
在父組件中引入并傳值使用:
<template> ?? ?<toggle-switch :isOpen="systemConfig.enable_email" @changeSwitch="changeSwitch" v-model="systemConfig.enable_email"></toggle-switch> </template> <script> import toggleSwitch from '../../components/switch.vue'; export default { ? ? data() { ? ? ? ? return {} ? ? }, ? ? components: { ? ? ? ? toggleSwitch ? ? }, ? ? methods: { ? ? ? ? changeSwitch() { ? ? ? ? ? this.systemConfig.enable_email = !this.systemConfig.enable_email; ? ? ? ? } ? ? } }
效果圖:
注:
父組件向子組件傳值,可以直接通過(guò):isOpen="systemConfig.enable_email"
傳遞數(shù)據(jù),子組件通過(guò) prop 接收數(shù)據(jù):props: ["isOpen"],
;
但子組件不能直接修改父組件的數(shù)據(jù),可以通過(guò) $emit
調(diào)用父組件的方法來(lái)修改父組件的數(shù)據(jù),$emit
的第一個(gè)參數(shù)要與父組件 @
后的名稱(chēng)保持一致。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 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實(shí)現(xiàn)移動(dòng)端的開(kāi)關(guān)按鈕
- vue.js封裝switch開(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+ElementUI實(shí)現(xiàn)表單動(dòng)態(tài)渲染、可視化配置的方法
這篇文章主要介紹了Vue+ElementUI實(shí)現(xiàn)表單動(dòng)態(tài)渲染、可視化配置的方法,需要的朋友可以參考下2018-03-03vue結(jié)合leaflet實(shí)現(xiàn)地圖放大鏡
放大鏡在很多地方都可以使用的到,本文主要介紹了vue結(jié)合leaflet實(shí)現(xiàn)地圖放大鏡,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06安裝vue-cli報(bào)錯(cuò) -4058 的解決方法
這篇文章主要介紹了安裝vue-cli報(bào)錯(cuò) -4058 的解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10vue修改this.$confirm的文字樣式、自定義樣式代碼實(shí)例
this.$confirm是一個(gè)?Vue.js?中的彈窗組件,其樣式可以通過(guò)?CSS?進(jìn)行自定義,下面這篇文章主要給大家介紹了關(guān)于vue修改this.$confirm的文字樣式、自定義樣式的相關(guān)資料,需要的朋友可以參考下2024-02-02vue導(dǎo)出word純前端的實(shí)現(xiàn)方式
這篇文章主要介紹了vue導(dǎo)出word純前端的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04SpringBoot實(shí)現(xiàn)全局和局部跨域的兩種方式
本文主要介紹了SpringBoot實(shí)現(xiàn)全局和局部跨域的兩種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01