number-box.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. methods: {
  26. add() {
  27. this.number++
  28. this.$emit('change', this.number)
  29. },
  30. sub() {
  31. if (this.number > 1) {
  32. this.number--
  33. this.$emit('change', this.number)
  34. } else {
  35. this.$util.msg('购买数量不能少于1', 2000)
  36. }
  37. },
  38. blur() {
  39. if (this.number <= 0) {
  40. this.number = 1
  41. this.$util.msg('购买数量不能少于1', 2000)
  42. }
  43. this.number = parseInt(this.number)
  44. this.$emit('change', this.number)
  45. }
  46. }
  47. }
  48. </script>
  49. <style lang="scss" scoped>
  50. .number-box {
  51. width: 148rpx;
  52. height: 48rpx;
  53. box-sizing: border-box;
  54. display: flex;
  55. justify-content: space-between;
  56. align-items: center;
  57. border-radius: 24rpx;
  58. border: 1rpx solid #e1e1e1;
  59. .sub,
  60. .add {
  61. flex: 1;
  62. text-align: center;
  63. font-size: 28rpx;
  64. color: #ccc;
  65. }
  66. .number {
  67. text-align: center;
  68. width: 56rpx;
  69. font-size: 26rpx;
  70. font-weight: 500;
  71. color: #333333;
  72. box-sizing: border-box;
  73. border-right: 1rpx solid #e1e1e1;
  74. border-left: 1rpx solid #e1e1e1;
  75. }
  76. }
  77. </style>