yd-text-price.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <template>
  2. <view>
  3. <text v-for="(item, index) in textArray" :key="index" :style="{ fontSize: (index === 1 ? integerSize : size) + 'px', color, textDecoration }">
  4. {{ item }}
  5. </text>
  6. </view>
  7. </template>
  8. <script>
  9. /**
  10. * 此组件简单的显示(驼峰式)的价格
  11. */
  12. export default {
  13. name: 'yd-text-price',
  14. components: {},
  15. props: {
  16. //货币符号
  17. symbol: {
  18. type: String,
  19. default: '¥'
  20. },
  21. price: {
  22. type: [String, Number],
  23. default: ''
  24. },
  25. color: {
  26. type: String,
  27. default: '#333333'
  28. },
  29. //字体大小
  30. size: {
  31. type: [String, Number],
  32. default: 15
  33. },
  34. //整形部分字体大小可单独定义
  35. intSize: {
  36. type: [String, Number],
  37. default: ''
  38. },
  39. //文字装饰,下划线,中划线等
  40. decoration: {
  41. type: String,
  42. default: 'none'
  43. }
  44. },
  45. data() {
  46. return {
  47. textDecoration: this.decoration
  48. }
  49. },
  50. computed: {
  51. textArray() {
  52. let array = []
  53. if (this.price === '' || this.price === undefined) {
  54. return []
  55. }
  56. array.push(this.symbol)
  57. if (!/^\d+(\.\d+)?$/.test(this.price)) {
  58. console.error('组件<yd-text-price :text="???" 此处参数应为金额数字')
  59. } else {
  60. let arr = parseFloat(this.price).toFixed(2).split('.')
  61. array.push(arr[0])
  62. array.push('.' + arr[1])
  63. }
  64. return array
  65. },
  66. integerSize() {
  67. return this.intSize ? this.intSize : this.size
  68. }
  69. }
  70. }
  71. </script>
  72. <style scoped></style>