handle.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { nextTick } from 'vue';
  2. export default <LibMixins>{
  3. data(){
  4. return {
  5. vUseTabData:undefined,
  6. vUseTabSelect:undefined,
  7. swipeOption:undefined
  8. }
  9. },
  10. computed:{
  11. swiper(){
  12. if (this.$refs.swiper) {
  13. return this.$refs.swiper.swiper;
  14. }
  15. return undefined;
  16. },
  17. // 当前索引的下标
  18. vTabSelect:{
  19. get(this:any):number{
  20. if (this.vUseTabSelect === undefined) {
  21. this.vUseTabSelect = this.value === undefined ? this.defaultValue : this.value;
  22. }
  23. return this.vUseTabSelect;
  24. },
  25. set(this:any,value:number) {
  26. if (this.vUseTabSelect !== value) {
  27. this.vUseTabSelect = value;
  28. if (this.vUseTabData && this.vUseTabData[value].loading !== true) {
  29. this.vUseTabData[value].loading = true;
  30. }
  31. // 查看是否需要变更 swiper
  32. if (this.swiper && this.swiper.realIndex !== value) {
  33. this.swiper.slideTo(value);
  34. }
  35. // 执行change
  36. if (this.value !== value) {
  37. return this.$emit('input',value);
  38. }
  39. }
  40. }
  41. },
  42. // 当前使用的 data
  43. vTabData():Array<tabDataObject> {
  44. if (this.vUseTabData === undefined) {
  45. this.vUseTabData = (this.data as tabData).map((item,index):tabDataObject=>{
  46. let result:tabDataObject;
  47. if (typeof item === 'string') {
  48. result = {
  49. label: item,
  50. slot:'',
  51. loading: false
  52. }
  53. } else {
  54. result = item;
  55. }
  56. // 设置 loading
  57. if (!result.loading) {
  58. result.loading = !(index !== this.vTabSelect && this.lazy);
  59. }
  60. // 设置插槽
  61. result.slot = result.slot || 'tab-'+index;
  62. return result;
  63. });
  64. }
  65. return this.vUseTabData;
  66. }
  67. },
  68. watch:{
  69. value:function (){
  70. return this.triggerTo(this.value);
  71. }
  72. },
  73. methods:{
  74. triggerTo(index:number){
  75. if (this.vUseTabSelect !== index) {
  76. this.vTabSelect = index;
  77. }
  78. },
  79. updateAutoHeight(){
  80. if (this.autoHeight) {
  81. nextTick(()=> {
  82. this.swiper && this.swiper.updateAutoHeight();
  83. })
  84. }
  85. }
  86. },
  87. mounted() {
  88. this.updateAutoHeight();
  89. },
  90. created() {
  91. this.swipeOption = {
  92. initialSlide: this.vTabSelect,
  93. allowTouchMove: this.touchMove,
  94. speed: this.speed,
  95. autoHeight: this.autoHeight,
  96. resizeObserver: this.autoHeight,
  97. on:{
  98. slideChangeTransitionStart: (swiper: any)=>{
  99. // 设置执行下标
  100. this.vTabSelect = swiper.realIndex;
  101. }
  102. }
  103. }
  104. }
  105. }