price-format.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <text :style="{color, 'font-weight': weight}" :class="(lineThrough ? 'line-through' : '') + ' price-format'">
  3. <text v-if="showSubscript" :style="{'font-size': subscriptSize + 'rpx', 'margin-right': '2rpx'}">¥</text>
  4. <text :style="{'font-size': firstSize + 'rpx', 'margin-right': '1rpx'}">{{priceSlice.first}}</text>
  5. <text v-if="priceSlice.second" :style="{'font-size': secondSize + 'rpx'}">.{{priceSlice.second}}</text>
  6. </text>
  7. </template>
  8. <script>
  9. export default {
  10. data() {
  11. return {
  12. priceSlice: {
  13. }
  14. };
  15. },
  16. components: {},
  17. props: {
  18. firstSize: {
  19. type: [String, Number],
  20. default: 28
  21. },
  22. secondSize: {
  23. type: [String, Number],
  24. default: 28
  25. },
  26. color: {
  27. type: String
  28. },
  29. weight: {
  30. type:[String, Number] ,
  31. default: 400
  32. },
  33. price: {
  34. type: [String, Number],
  35. default: ""
  36. },
  37. showSubscript: {
  38. type: Boolean,
  39. default: true
  40. },
  41. subscriptSize: {
  42. type: [String, Number],
  43. default: 28
  44. },
  45. lineThrough: {
  46. type: Boolean,
  47. default: false
  48. }
  49. },
  50. created() {
  51. this.priceFormat()
  52. },
  53. watch: {
  54. price(val) {
  55. this.priceFormat()
  56. }
  57. },
  58. methods: {
  59. priceFormat() {
  60. let {
  61. price
  62. } = this;
  63. let priceSlice = {}
  64. if(price !== null && price !== '' && price !== undefined) {
  65. price = parseFloat(price);
  66. price = String(price).split('.');
  67. priceSlice.first = price[0];
  68. priceSlice.second = price[1];
  69. this.priceSlice = priceSlice
  70. }else {
  71. this.priceSlice = {
  72. first: 0
  73. }
  74. }
  75. }
  76. }
  77. };
  78. </script>
  79. <style>
  80. .price-format {
  81. font-family: Avenir, SourceHanSansCN, PingFang SC, Arial, Hiragino Sans GB, Microsoft YaHei, sans-serif;
  82. }
  83. </style>