index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. Component({
  2. data: {
  3. selected: 0,
  4. color: "#412A12",
  5. selectedColor: "#B1967B",
  6. borderStyle: "black",
  7. backgroundColor: "#ffffff",
  8. isShowTabar: true,
  9. list: [
  10. {
  11. "pagePath": "/pages/index/index",
  12. "iconPath": "/imgs/icon_home_normal.png",
  13. "selectedIconPath": "/imgs/icon_home_active.png",
  14. "text": "首页"
  15. },
  16. {
  17. "pagePath": "/pages/exhibition/index",
  18. "iconPath": "/imgs/icon_exhibition_normal.png",
  19. "selectedIconPath": "/imgs/icon_exhibition_active.png",
  20. "text": "展览"
  21. },
  22. {
  23. "pagePath": "/pages/cover/index",
  24. "iconPath": "/imgs/icon_ar.png",
  25. "selectedIconPath": "/imgs/icon_ar.png",
  26. "text": "钻头列阵",
  27. "center": true,
  28. },
  29. {
  30. "pagePath": "/pages/collection/index",
  31. "iconPath": "/imgs/icon_culture_normal.png",
  32. "selectedIconPath": "/imgs/icon_culture_active.png",
  33. "text": "藏品"
  34. },
  35. {
  36. "pagePath": "/pages/user/index",
  37. "iconPath": "/imgs/icon_user_normal.png",
  38. "selectedIconPath": "/imgs/icon_user_active.png",
  39. "text": "我的"
  40. }
  41. ]
  42. },
  43. attached() {
  44. const pages = getCurrentPages() || []
  45. const current = pages[pages.length - 1]
  46. const route = current ? ('/' + current.route) : ''
  47. const idx = this.data.list.findIndex(i => i.pagePath === route)
  48. if (idx !== -1) {
  49. this.setData({ selected: idx })
  50. }
  51. },
  52. methods: {
  53. switchTab(e) {
  54. const data = e.currentTarget.dataset
  55. const url = data.path
  56. const item = this.data.list[data.index]
  57. if (item && item.navigate) {
  58. wx.navigateTo({ url })
  59. } else {
  60. const pages = getCurrentPages() || []
  61. const current = pages[pages.length - 1]
  62. const currentRoute = current ? ('/' + current.route) : ''
  63. // 如果已在当前页,避免重复切换导致页面重载与导航闪烁
  64. if (currentRoute === url) {
  65. this.setData({ selected: data.index })
  66. return
  67. }
  68. wx.switchTab({ url })
  69. // this.setData({ selected: data.index })
  70. }
  71. }
  72. }
  73. })