tabbar.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // pages/luckDraw/components/tabbar.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. },
  8. /**
  9. * 组件的初始数据
  10. */
  11. data: {
  12. list: [
  13. {
  14. text: "首页",
  15. pagePath: "/pages/luckDraw/index",
  16. selectedIconPath: "../../../images/luck-draw/home-active.png",
  17. iconPath: "../../../images/luck-draw/home.png",
  18. },
  19. {
  20. text: "我的",
  21. pagePath: "/pages/luckDraw/profile",
  22. selectedIconPath: "../../../images/luck-draw/user-active.png",
  23. iconPath: "../../../images/luck-draw/user.png",
  24. }
  25. ]
  26. },
  27. attached: function() {
  28. var url =getCurrentPages()[getCurrentPages().length-1].route
  29. this.data.list.forEach(v => {
  30. if (this.getPathName(v.pagePath) == this.getPathName(url)) {
  31. v.active = true
  32. }
  33. })
  34. this.setData({
  35. list: this.data.list
  36. })
  37. },
  38. /**
  39. * 组件的方法列表
  40. */
  41. methods: {
  42. toPage: function(e) {
  43. const index = e.currentTarget.dataset.index;
  44. this.data.list.forEach((v, i) => {
  45. if (i == index) {
  46. v.active = true
  47. } else {
  48. v.active = false
  49. }
  50. })
  51. this.setData({
  52. list: this.data.list
  53. })
  54. if (index == 0) {
  55. wx.reLaunch({
  56. url: this.data.list[index].pagePath,
  57. })
  58. return
  59. }
  60. wx.redirectTo({
  61. url: this.data.list[index].pagePath,
  62. })
  63. },
  64. getPathName(path) {
  65. let pos = path.indexOf("luckDraw/");
  66. pos += "luckDraw/".length;
  67. return path.substr(pos)
  68. }
  69. }
  70. })