vue自定義開關組件使用詳解
本文實例為大家分享了vue自定義開關組件的具體代碼,供大家參考,具體內(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ù),要通過$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; ? ? ? ? } ? ? } }
效果圖:
注:
父組件向子組件傳值,可以直接通過:isOpen="systemConfig.enable_email"
傳遞數(shù)據(jù),子組件通過 prop 接收數(shù)據(jù):props: ["isOpen"],
;
但子組件不能直接修改父組件的數(shù)據(jù),可以通過 $emit
調用父組件的方法來修改父組件的數(shù)據(jù),$emit
的第一個參數(shù)要與父組件 @
后的名稱保持一致。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Vue+ElementUI實現(xiàn)表單動態(tài)渲染、可視化配置的方法
這篇文章主要介紹了Vue+ElementUI實現(xiàn)表單動態(tài)渲染、可視化配置的方法,需要的朋友可以參考下2018-03-03vue修改this.$confirm的文字樣式、自定義樣式代碼實例
this.$confirm是一個?Vue.js?中的彈窗組件,其樣式可以通過?CSS?進行自定義,下面這篇文章主要給大家介紹了關于vue修改this.$confirm的文字樣式、自定義樣式的相關資料,需要的朋友可以參考下2024-02-02SpringBoot實現(xiàn)全局和局部跨域的兩種方式
本文主要介紹了SpringBoot實現(xiàn)全局和局部跨域的兩種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01