litian
2024-03-25 1fd211c2528c8ba7fff173c60b31acbc5807c809
custom-tab-bar/index.js
@@ -1,35 +1,34 @@
import TabMenu from './data';
Component({
  data: {
    active: 0,
    selected: 0,
    list: TabMenu,
  },
  observers: {
    'active': function (newValue) {
      console.log('active变化', newValue);
    }
  },
  // observers: {
  //   'active': function (newValue) {
  //     console.log('active变化', newValue);
  //   }
  // },
  methods: {
    onChange(event) {
      console.log(event);
      this.setData({ active: this.data.list[event.detail.value].id });
      const url = this.data.list[event.detail.value].url.startsWith('/')
        ? this.data.list[event.detail.value].url
        : `/${this.data.list[event.detail.value].url}`
    changeTab(item) {
      this.setData({
        selected: item.currentTarget.dataset.num
      })
      wx.switchTab({
        url: url
        url: this.data.list[this.data.selected].url.startsWith('/') ?
          this.data.list[this.data.selected].url : `/${this.data.list[this.data.selected].url}`,
      });
    },
    init() {
      const page = getCurrentPages().pop();
      const route = page ? page.route.split('?')[0] : '';
      const active = this.data.list.findIndex(
        (item) =>
          (item.url.startsWith('/') ? item.url.substr(1) : item.url) ===
          `${route}`,
      );
      this.setData({ active });
    },
    // init() {
    //   const page = getCurrentPages().pop();
    //   const route = page ? page.route.split('?')[0] : '';
    //   const active = this.data.list.findIndex(
    //     (item) =>
    //       (item.url.startsWith('/') ? item.url.substr(1) : item.url) ===
    //       `${route}`,
    //   );
    //   this.setData({ active });
    // },
  },
});