number-box.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <view class="number-box">
  3. <view class="sub iconfont icon-jianhao" @click="sub"></view>
  4. <input class="number" type="number" v-model="number" @blur="blur" />
  5. <view class="add iconfont icon-jiahao" @click="add"></view>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. name: 'number-box',
  11. data() {
  12. return {
  13. number: 0
  14. }
  15. },
  16. props: {
  17. value: {
  18. type: Number,
  19. default: 0
  20. }
  21. },
  22. created() {
  23. this.number = this.value
  24. },
  25. watch:{
  26. value(val){
  27. this.number = val
  28. }
  29. },
  30. methods: {
  31. add() {
  32. this.number++
  33. this.$emit('change', this.number)
  34. },
  35. sub() {
  36. if (this.number > 1) {
  37. this.number--
  38. this.$emit('change', this.number)
  39. } else {
  40. this.$util.msg('购买数量不能少于1', 2000)
  41. }
  42. },
  43. blur() {
  44. if (this.number <= 0) {
  45. this.number = 1
  46. this.$util.msg('购买数量不能少于1', 2000)
  47. }
  48. this.number = parseInt(this.number)
  49. this.$emit('change', this.number)
  50. }
  51. }
  52. }
  53. </script>
  54. <style lang="scss" scoped>
  55. .number-box {
  56. width: 148rpx;
  57. height: 48rpx;
  58. box-sizing: border-box;
  59. display: flex;
  60. justify-content: space-between;
  61. align-items: center;
  62. border-radius: 24rpx;
  63. border: 1rpx solid #e1e1e1;
  64. .sub,
  65. .add {
  66. flex: 1;
  67. text-align: center;
  68. font-size: 28rpx;
  69. color: #ccc;
  70. }
  71. .number {
  72. text-align: center;
  73. width: 56rpx;
  74. font-size: 26rpx;
  75. font-weight: 500;
  76. color: #333333;
  77. box-sizing: border-box;
  78. border-right: 1rpx solid #e1e1e1;
  79. border-left: 1rpx solid #e1e1e1;
  80. }
  81. }
  82. </style>