uni-title.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. props: {
  27. type: {
  28. type: String,
  29. default: ''
  30. },
  31. title: {
  32. type: String,
  33. default: ''
  34. },
  35. align: {
  36. type: String,
  37. default: 'left'
  38. },
  39. color: {
  40. type: String,
  41. default: '#333333'
  42. },
  43. stat: {
  44. type: [Boolean, String],
  45. default: ''
  46. }
  47. },
  48. data() {
  49. return {
  50. };
  51. },
  52. computed: {
  53. textAlign() {
  54. let align = 'center';
  55. switch (this.align) {
  56. case 'left':
  57. align = 'flex-start'
  58. break;
  59. case 'center':
  60. align = 'center'
  61. break;
  62. case 'right':
  63. align = 'flex-end'
  64. break;
  65. }
  66. return align
  67. }
  68. },
  69. watch: {
  70. title(newVal) {
  71. if (this.isOpenStat()) {
  72. // 上报数据
  73. if (uni.report) {
  74. uni.report('title', this.title)
  75. }
  76. }
  77. }
  78. },
  79. mounted() {
  80. if (this.isOpenStat()) {
  81. // 上报数据
  82. if (uni.report) {
  83. uni.report('title', this.title)
  84. }
  85. }
  86. },
  87. methods: {
  88. isOpenStat() {
  89. if (this.stat === '') {
  90. this.isStat = false
  91. }
  92. let stat_type = (typeof(this.stat) === 'boolean' && this.stat) || (typeof(this.stat) === 'string' && this.stat !==
  93. '')
  94. if (this.type === "") {
  95. this.isStat = true
  96. if (this.stat.toString() === 'false') {
  97. this.isStat = false
  98. }
  99. }
  100. if (this.type !== '') {
  101. this.isStat = true
  102. if (stat_type) {
  103. this.isStat = true
  104. } else {
  105. this.isStat = false
  106. }
  107. }
  108. return this.isStat
  109. }
  110. }
  111. }
  112. </script>
  113. <style scoped>
  114. /* .uni-title {
  115. } */
  116. .uni-title__box {
  117. /* #ifndef APP-NVUE */
  118. display: flex;
  119. /* #endif */
  120. flex-direction: column;
  121. align-items: flex-start;
  122. justify-content: center;
  123. padding: 8px 0;
  124. flex: 1;
  125. }
  126. .uni-title__base {
  127. font-size: 15px;
  128. color: #333;
  129. font-weight: 500;
  130. }
  131. .uni-h1 {
  132. font-size: 20px;
  133. color: #333;
  134. font-weight: bold;
  135. }
  136. .uni-h2 {
  137. font-size: 18px;
  138. color: #333;
  139. font-weight: bold;
  140. }
  141. .uni-h3 {
  142. font-size: 16px;
  143. color: #333;
  144. font-weight: bold;
  145. /* font-weight: 400; */
  146. }
  147. .uni-h4 {
  148. font-size: 14px;
  149. color: #333;
  150. font-weight: bold;
  151. /* font-weight: 300; */
  152. }
  153. .uni-h5 {
  154. font-size: 12px;
  155. color: #333;
  156. font-weight: bold;
  157. /* font-weight: 200; */
  158. }
  159. </style>