当前位置:   article > 正文

原生微信小程序使用vant的tabbar(解决点击俩次图标才正确切换)_小程序 van-tabbar

小程序 van-tabbar

一、声明:在导入使用vant (tabbar)组件的时候,发现通过点击切换的方法来更改active的方法,会出现图标没用及时对应上,需要第二次点击才对应上的问题。

先使用npm引入vant组件库

1.第一步:在资源管理器下方空白处选择”在外部终端窗口打开“

2.第二步:使用命令初始

npm init

回车操作后,会出现以下,如果没有什么更改,一直回车即可

初始化后在项目中会出现一个绿色的package.json文件则表示初始成功了!

3.第三步:安装vant

4.第四步:安装好后在微信开发者工具操作栏,点击工具进行构建npm操作

安装的vant就在node_modules模块中

5.第五步:开启自定义tabbar(添加“custom":true)

”custom“:true

6.与pages同名创建custom-tab-bar文件夹(名字不能变),并创建index组件(tabbar自定义组件)

tabbar自定义组件——index.js代码:

  1. // custom-tab-bar/index.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. options: {
  7. // 开启这个才可以修改vant组件里面的样式
  8. styleIsolation: 'shared'
  9. },
  10. properties: {
  11. },
  12. /**
  13. * 组件的初始数据
  14. */
  15. data: {
  16. active: null,
  17. "list": [
  18. {
  19. "pagePath": "/pages/heart/heart",
  20. "text": "心声",
  21. "iconPath": "/static/tabbar-icon/heart.png",
  22. "selectedIconPath": "/static/tabbar-icon/heart-active.png"
  23. },
  24. {
  25. "pagePath": "/pages/index/index",
  26. "text": "首页",
  27. "iconPath": "/static/tabbar-icon/home.png",
  28. "selectedIconPath": "/static/tabbar-icon/home-active.png"
  29. },
  30. {
  31. "pagePath": "/pages/classify/classify",
  32. "text": "分类",
  33. "iconPath": "/static/tabbar-icon/classfiy.png",
  34. "selectedIconPath": "/static/tabbar-icon/classfiy-active.png"
  35. },
  36. {
  37. "pagePath": "/pages/cart/cart",
  38. "text": "购物车",
  39. "iconPath": "/static/tabbar-icon/cart.png",
  40. "selectedIconPath": "/static/tabbar-icon/cart-active.png",
  41. info: 2
  42. },
  43. {
  44. "pagePath": "/pages/my/my",
  45. "text": "我的",
  46. "iconPath": "/static/tabbar-icon/my.png",
  47. "selectedIconPath": "/static/tabbar-icon/my-active.png"
  48. }
  49. ]
  50. },
  51. /**
  52. * 组件的方法列表
  53. */
  54. methods: {
  55. // 切换tabbar页面
  56. onChange(event) {
  57. wx.switchTab({
  58. url: this.data.list[event.detail].pagePath,
  59. })
  60. }
  61. }
  62. })

tabbar自定义组件——index.wxml代码:

  1. <view>
  2. <van-tabbar active="{{ active }}" bind:change="onChange">
  3. <van-tabbar-item wx:for="{{list}}" wx:key="index" info="{{item.info? item.info:''}}">
  4. <image slot="icon" src="{{item.iconPath}}" mode="aspectFit" style="width: 50rpx;height: 50rpx;"/>
  5. <image slot="icon-active" src="{{item.selectedIconPath}}" mode="aspectFit" style="width: 50rpx;height: 50rpx;"/>
  6. {{item.text}}
  7. </van-tabbar-item>
  8. </van-tabbar>
  9. </view>

tabbar自定义组件——index.wxss:(使用变量改组件样式)

  1. /* custom-tab-bar/index.wxss */
  2. .van-tabbar-item {
  3. --tabbar-item-margin-bottom:0;
  4. }

对于使用该组件的问题出现在下面:

  1. // 切换tabbar页面
  2. onChange(event) {
  3. this.setData({
  4. active:event.detail
  5. })
  6. wx.switchTab({
  7. url: this.data.list[event.detail].pagePath,
  8. })
  9. }
  10. }

问题出现在这里:

  1. this.setData({
  2. active:event.detail
  3. })

二、解决:在切换tabbar的时候,我们采取另外一种方法思路:

在每一个页面使用tabbar自定义组件的地方,在onshow生命周期函数里初始active的值,用来对应每个页面切换之后展示对应的图标。

我这里有五个tabbar

假如,我采用index页面作为一个tabbar页,对应的active可以这么更改其选中态

  1. /**
  2. * 生命周期函数--监听页面显示
  3. */
  4. onShow() {
  5. // 通过 getTabBar 接口获取组件实例,并调用 setData 更新选中态
  6. this.getTabBar().setData({active: 0})
  7. },

第二个tabbar设置也是如此:

  1. /**
  2. * 生命周期函数--监听页面显示
  3. */
  4. onShow() {
  5. // 通过 getTabBar 接口获取组件实例,并调用 setData 更新选中态
  6. this.getTabBar().setData({active: 1})
  7. },

依次设置,这样图标就会对应每一页面

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/89954
推荐阅读