uni-title.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <view class="uni-title__box" :style="{'align-items':textAlign}">
  3. <text class="uni-title__base" :class="['uni-'+type]" :style="{'color':color}">{{title}}</text>
  4. </view>
  5. </template>
  6. <script>
  7. /**
  8. * Title 标题
  9. * @description 标题,通常用于记录页面标题,使用当前组件,uni-app 如果开启统计,将会自动统计页面标题
  10. * @tutorial https://ext.dcloud.net.cn/plugin?id=1066
  11. * @property {String} type = [h1|h2|h3|h4|h5] 标题类型
  12. * @value h1 一级标题
  13. * @value h2 二级标题
  14. * @value h3 三级标题
  15. * @value h4 四级标题
  16. * @value h5 五级标题
  17. * @property {String} title 标题内容
  18. * @property {String} align = [left|center|right] 对齐方式
  19. * @value left 做对齐
  20. * @value center 居中对齐
  21. * @value right 右对齐
  22. * @property {String} color 字体颜色
  23. * @property {Boolean} stat = [true|false] 是否开启统计功能呢,如不填写type值,默认为开启,填写 type 属性,默认为关闭
  24. */
  25. export default {
  26. name:"UniTitle",
  27. props: {
  28. type: {
  29. type: String,
  30. default: ''
  31. },
  32. title: {
  33. type: String,
  34. default: ''
  35. },
  36. align: {
  37. type: String,
  38. default: 'left'
  39. },
  40. color: {
  41. type: String,
  42. default: '#333333'
  43. },
  44. stat: {
  45. type: [Boolean, String],
  46. default: ''
  47. }
  48. },
  49. data() {
  50. return {
  51. };
  52. },
  53. computed: {
  54. textAlign() {
  55. let align = 'center';
  56. switch (this.align) {
  57. case 'left':
  58. align = 'flex-start'
  59. break;
  60. case 'center':
  61. align = 'center'
  62. break;
  63. case 'right':
  64. align = 'flex-end'
  65. break;
  66. }
  67. return align
  68. }
  69. },
  70. watch: {
  71. title(newVal) {
  72. if (this.isOpenStat()) {
  73. // 上报数据
  74. if (uni.report) {
  75. uni.report('title', this.title)
  76. }
  77. }
  78. }
  79. },
  80. mounted() {
  81. if (this.isOpenStat()) {
  82. // 上报数据
  83. if (uni.report) {
  84. uni.report('title', this.title)
  85. }
  86. }
  87. },
  88. methods: {
  89. isOpenStat() {
  90. if (this.stat === '') {
  91. this.isStat = false
  92. }
  93. let stat_type = (typeof(this.stat) === 'boolean' && this.stat) || (typeof(this.stat) === 'string' && this.stat !==
  94. '')
  95. if (this.type === "") {
  96. this.isStat = true
  97. if (this.stat.toString() === 'false') {
  98. this.isStat = false
  99. }
  100. }
  101. if (this.type !== '') {
  102. this.isStat = true
  103. if (stat_type) {
  104. this.isStat = true
  105. } else {
  106. this.isStat = false
  107. }
  108. }
  109. return this.isStat
  110. }
  111. }
  112. }
  113. </script>
  114. <style>
  115. /* .uni-title {
  116. } */
  117. .uni-title__box {
  118. /* #ifndef APP-NVUE */
  119. display: flex;
  120. /* #endif */
  121. flex-direction: column;
  122. align-items: flex-start;
  123. justify-content: center;
  124. padding: 8px 0;
  125. flex: 1;
  126. }
  127. .uni-title__base {
  128. font-size: 15px;
  129. color: #333;
  130. font-weight: 500;
  131. }
  132. .uni-h1 {
  133. font-size: 20px;
  134. color: #333;
  135. font-weight: bold;
  136. }
  137. .uni-h2 {
  138. font-size: 18px;
  139. color: #333;
  140. font-weight: bold;
  141. }
  142. .uni-h3 {
  143. font-size: 16px;
  144. color: #333;
  145. font-weight: bold;
  146. /* font-weight: 400; */
  147. }
  148. .uni-h4 {
  149. font-size: 14px;
  150. color: #333;
  151. font-weight: bold;
  152. /* font-weight: 300; */
  153. }
  154. .uni-h5 {
  155. font-size: 12px;
  156. color: #333;
  157. font-weight: bold;
  158. /* font-weight: 200; */
  159. }
  160. </style>