u-tabs-item.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <view class="u-tabs-item" :style="[$u.addStyle(customStyle)]">
  3. <template v-if="isActive">
  4. <slot />
  5. </template>
  6. </view>
  7. </template>
  8. <script>
  9. import props from './props.js';
  10. import mixin from '../../libs/mixin/mixin'
  11. import mpMixin from '../../libs/mixin/mpMixin';
  12. /**
  13. * TabsItem tabs标签组件的子组件
  14. * @description tabs标签组件的子组件,用于定义单个标签页的内容
  15. * @tutorial https://uview.d3u.cn/components/tabs.html
  16. * @property {String | Number} name 标签的名称,作为与u-tabs的current参数匹配的标识符
  17. * @property {String} label 标签显示的文本
  18. * @property {Boolean} disabled 是否禁用此标签(默认 false )
  19. * @property {String | Number} badge 右上角的角标提示信息
  20. * @property {Boolean} dot 是否显示圆点,将会覆盖badge参数(默认 false )
  21. * @property {Object} customStyle 定义需要用到的外部样式
  22. * @example <u-tabs-item name="tab1" label="标签1">内容1</u-tabs-item>
  23. */
  24. export default {
  25. name: 'u-tabs-item',
  26. mixins: [mpMixin, mixin, props],
  27. data() {
  28. return {
  29. isActive: false, // 是否处于激活状态
  30. parentData: {
  31. innerCurrent: 0,
  32. current: null,
  33. label: ''
  34. }
  35. }
  36. },
  37. created() {
  38. this.init()
  39. },
  40. methods: {
  41. init() {
  42. // 支付宝小程序不支持provide/inject,所以使用这个方法获取整个父组件,在created定义,避免循环引用
  43. this.updateParentData()
  44. if (!this.parent) {
  45. uni.$u.error('u-tabs-item必须搭配u-tabs组件使用')
  46. }
  47. // 本子组件在u-tabs的children数组中的索引
  48. const index = this.parent.children.indexOf(this)
  49. this.isActive = index === this.parentData.innerCurrent
  50. },
  51. updateParentData() {
  52. // 此方法在mixin中
  53. this.getParentData('u-tabs')
  54. },
  55. // 此方法将会被父组件u-tabs调用
  56. updateFromParent() {
  57. // 重新初始化
  58. this.init()
  59. }
  60. },
  61. }
  62. </script>
  63. <style lang="scss" scoped>
  64. @import "../../libs/css/components.scss";
  65. .u-tabs-item {
  66. width: 100%;
  67. height: 100%;
  68. }
  69. </style>