vue.js封装switch开关组件

我的项目本来用的element,但是switch开关不符合设计要求,于是自己封装了一个switch组件,并且实现了switch开关的双向数据绑定

<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>

调用开关组件

<toggle-switch
              v-model="value"
              :value.sync="value"
              @change="switchChange"
            >
            </toggle-switch>

组件实现了switch开关的双向数据绑定,在父组件的methods中写一个@change事件

switchChange() {
      console.log("switch值改变");
    },

欢迎关注我的个人技术公众号!javascript艺术

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vue.js允许您封装可重用的组件并在应用程序中多次使用它们。以下是单独封装和引用Vue组件的基本步骤: 1. 创建组件 使用Vue.js创建一个组件。您可以将其放在一个单独的文件中,并通过导出来暴露组件。例如,假设您正在创建一个名为"MyComponent"的组件。可以创建一个名为"MyComponent.vue"的文件,并使用以下代码: ``` <template> <div> <!-- 组件内容 --> </div> </template> <script> export default { name: 'MyComponent', // 组件的逻辑 } </script> <style> /* 可选的组件样式 */ </style> ``` 2. 导入组件组件导入到您的应用程序中。在Vue.js中,您可以使用import语句从文件中导入组件。在您的Vue应用程序中使用以下代码: ``` import MyComponent from './MyComponent.vue'; export default { name: 'App', components: { MyComponent } } ``` 3. 使用组件 在您的应用程序中使用组件。一旦将组件导入到应用程序中,您可以在应用程序的模板中使用它。例如,在App.vue中,您可以使用以下代码: ``` <template> <div> <my-component></my-component> </div> </template> <script> import MyComponent from './MyComponent.vue'; export default { name: 'App', components: { MyComponent } } </script> <style> /* 可选的样式 */ </style> ``` 在上面的示例中,您可以看到在模板中使用`<my-component>`标签来使用组件。该标签会自动渲染"MyComponent"组件的内容。 注意,您需要在组件的定义中设置正确的名称,以便在使用组件时引用它。在上面的示例中,组件名称为"MyComponent",因此您可以在模板中使用`<my-component>`标记。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值