vue 计数器组件封装

模仿element-ui计数器封装的组件

子组件 wmmInput.vue

<template>
  <div>
    <button class="buttonStyle" @click="reduce">-</button>
    <input class="inputStyle" type="text" :disabled="inputNumberDisabled" v-model="currentValue" @click="handleChange()"/>
    <button class="buttonStyle" @click="add">+</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentValue: 0
    };
  },
  props: {
    value: {
      type: Number,
      default: 0
    },
    step: {
      type: Number,
      default: 1
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    inputNumberDisabled() {
      return this.disabled;
    }
  },
  mounted() {
    // 更新当前值
    this.updateValue(this.value);
  },
  methods: {
    reduce() {
      this.currentValue -= this.step;
      this.$emit("change", this.currentValue);
    },
    add() {
      this.currentValue += this.step;
      this.$emit("change", this.currentValue);
    },
    handleChange(val) {
      //将自定义的change事件传给父组件
      this.$emit("change", this.currentValue);
    },
    updateValue(val) {
      this.currentValue = val;
    }
  }
};
</script>

<style scoped>
.inputStyle {
  width: 60px;
  text-align: center;
  height: 35px;
  line-height: 35px;
  background-color: #fff;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  box-sizing: border-box;
  color: #606266;
  cursor: pointer;
}
.buttonStyle {
  width: 30px;
  height: 35px;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  color: #606266;
  background: #f5f7fa;
  cursor: pointer;
}
</style>

父组件调用 index.vue

<template>
  <div>
    <h3 style="text-align: center;">InputNumber 计数器</h3>
    <wmmInput v-model="value" :disabled="false" :step="2" @change="handleChange"></wmmInput>
  </div>
</template>

<script>
import wmmInput from "@/components/wmmInput";
export default {
  components:{
      wmmInput
  },
  data () {
    return {
     value:0
    }
  },
  methods:{
    handleChange(val) {
      console.log(val);
    },
  }
}
</script>

<style scoped>

</style>

目前只封装了step,disabled属性和change事件。
我的详细代码指路 ➡ github地址

element 计数器源码地址

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现一个自定义的计数器组件,可以将上面的代码封装一个单独的组件。具体实现可以参考下面的代码: ``` <template> <div> <button @click="addCount">+2</button> <span>{{ count }}</span> </div> </template> <script> export default { // 定义组件的属性 props: { initValue: { type: Number, default: 5 }, minValue: { type: Number, default: 1 }, maxValue: { type: Number, default: 10 } }, data () { return { count: this.initValue } }, computed: { // 计算属性,根据计数器的值自动计算出新的值 newCount () { return this.count + 2 } }, methods: { // 点击按钮增加计数器的值 addCount () { if (this.count < this.maxValue - 1) { this.count += 2 } else { this.count = this.maxValue } } }, watch: { // 监听计数器的值,如果超出范围则重置为最小或最大值 count (newVal) { if (newVal < this.minValue) { this.count = this.minValue } else if (newVal > this.maxValue) { this.count = this.maxValue } } } } </script> ``` 在这个例子中,我们使用了 `props` 来定义组件的属性,包括 `initValue`、`minValue` 和 `maxValue`。在 `data` 中,我们将计数器的初始值设置为 `initValue`。在 `addCount` 方法中,我们根据 `maxValue` 来判断是否增加 2,同时,如果计数器的值超出了范围,则将其重置为最小或最大值。在 `watch` 中,我们监听计数器的值,如果超出范围则重置为最小或最大值。 使用这个自定义组件时,只需要在父组件中引入并传递相应的属性即可,如下所示: ``` <template> <div> <custom-counter :init-value="5" :min-value="1" :max-value="10"></custom-counter> </div> </template> <script> import CustomCounter from './CustomCounter.vue' export default { components: { CustomCounter } } </script> ``` 在这个例子中,我们通过 `:init-value`、`:min-value` 和 `:max-value` 来传递相应的属性值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值