index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { VantComponent } from '../common/component';
  2. import { iphonex } from '../mixins/iphonex';
  3. VantComponent({
  4. mixins: [iphonex],
  5. relation: {
  6. name: 'tabbar-item',
  7. type: 'descendant',
  8. linked(target) {
  9. this.data.items.push(target);
  10. setTimeout(() => {
  11. this.setActiveItem();
  12. });
  13. },
  14. unlinked(target) {
  15. this.data.items = this.data.items.filter(item => item !== target);
  16. setTimeout(() => {
  17. this.setActiveItem();
  18. });
  19. }
  20. },
  21. props: {
  22. active: Number,
  23. activeColor: String,
  24. fixed: {
  25. type: Boolean,
  26. value: true
  27. },
  28. zIndex: {
  29. type: Number,
  30. value: 1
  31. }
  32. },
  33. data: {
  34. items: []
  35. },
  36. watch: {
  37. active(active) {
  38. this.currentActive = active;
  39. this.setActiveItem();
  40. }
  41. },
  42. created() {
  43. this.currentActive = this.data.active;
  44. },
  45. methods: {
  46. setActiveItem() {
  47. this.data.items.forEach((item, index) => {
  48. item.setActive({
  49. active: index === this.currentActive,
  50. color: this.data.activeColor
  51. });
  52. });
  53. },
  54. onChange(child) {
  55. const active = this.data.items.indexOf(child);
  56. if (active !== this.currentActive && active !== -1) {
  57. this.$emit('change', active);
  58. this.currentActive = active;
  59. this.setActiveItem();
  60. }
  61. }
  62. }
  63. });