当前位置:   article > 正文

【小程序开发】基于Vant的tabBar案例_vant tabbar

vant tabbar

1.案例效果

sum=4,消息小红点显示为sum值

2.知识点

 3.实现步骤

基础能力 / 自定义 tabBar (qq.com)

4.实现代码

基于本地生活项目【小程序开发】实现本地生活项目-CSDN博客

4.1 实现tabBar数字徽标

效果:

过程:

list内容不删:

为了保证低版本兼容以及区分哪些页面是 tab 页,tabBar 的相关配置项需完整声明,但这些字段不会作用于自定义 tabBar 的渲染。

app.json添加 "custom": true

  1. {
  2. "tabBar": {
  3. "custom": true,
  4. "list": [{
  5. xxx
  6. }],
  7. xxx
  8. }

新建文件夹custom-tab-bar,新建组件index

配置npm 【小程序开发】npm包-CSDN博客 2Vant-Weapp

使用Vant Weapp内置TabBar组件:Tabbar 标签栏 - Vant Weapp (gitee.io)

 导入引入、基础语法、自定义图标代码

在徽标布局上,希望将文字和图片间距margin设置为0

点击图标:

在自定义组件中使用 Vant Weapp 组件时,需开启styleIsolation: 'shared'选项

代码:

app.json增加:

  1. {
  2. "usingComponents": {
  3. "van-button": "@vant/weapp/button/index",
  4. "van-tabbar": "@vant/weapp/tabbar/index",
  5. "van-tabbar-item": "@vant/weapp/tabbar-item/index"
  6. },

index.js

  1. Component({
  2. options:{
  3. styleIsolation:'shared'
  4. },
  5. data: {
  6. active: 0,
  7. "list": [{
  8. "pagePath": "pages/home/home",
  9. "text": "首页",
  10. "iconPath": "/images/008_咖啡.png",
  11. "selectedIconPath": "/images/008_奶茶奶盖.png"
  12. },
  13. {
  14. "pagePath": "pages/message/message",
  15. "text": "消息",
  16. "iconPath": "/images/008_披萨西餐.png",
  17. "selectedIconPath": "/images/008_汉堡快餐.png",
  18. info:2
  19. },
  20. {
  21. "pagePath": "pages/contact/contact",
  22. "text": "联系",
  23. "iconPath": "/images/008_火锅.png",
  24. "selectedIconPath": "/images/008_米粉面条.png"
  25. }
  26. ]
  27. },
  28. methods: {
  29. onChange(event) {
  30. // event.detail 的值为当前选中项的索引
  31. this.setData({ active: event.detail });
  32. },
  33. }
  34. })

index.wxml

  1. <van-tabbar active="{{ active }}" bind:change="onChange">
  2. <!-- info=3,徽标=3,info=0''不会被渲染成徽标 -->
  3. <van-tabbar-item wx:for="{{list}}" wx:key="index" info="{{item.info?item.info:''}}">
  4. <image
  5. slot="icon"
  6. src="{{item.iconPath}}"
  7. mode="aspectFit"
  8. style="width: 30px; height: 25px;"
  9. />
  10. <image
  11. slot="icon-active"
  12. src="{{item.selectedIconPath}}"
  13. mode="aspectFit"
  14. style="width: 30px; height: 25px;"
  15. />
  16. {{item.text}}
  17. </van-tabbar-item>
  18. </van-tabbar>

4.2 数字徽标值与组件的数值绑定

【小程序开发】全局数据共享-CSDN博客

引入store.js和numbers组件

index.js(配置tabBar)新增:

  1. import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
  2. import { store } from "../store/store";
  3. Component({
  4. behaviors:[storeBindingsBehavior],
  5. storeBindings:{
  6. store,
  7. fields:{
  8. sum:'sum',
  9. },
  10. },
  11. //与list数组中的info数据绑定
  12. observers:{
  13. 'sum':function (val) {
  14. this.setData({
  15. 'list[1].info':val
  16. })
  17. }
  18. xxx
  19. },

home.wxml

<my-numbers></my-numbers>

效果:

 

4.3 实现tabBar页面的切换效果

思路:

在index.wxml中有bind:change="onChange"监听tabBar变化

在index.js中:

  1. onChange(event) {
  2. // event.detail 的值为当前选中项的索引
  3. this.setData({ active: event.detail });
  4. },

利用索引拿到相应页面路径(以'/'开头)

利用

wx.switchTab({

  url: 'url',

})即可完成跳转

CODE

  1. methods: {
  2. onChange(event) {
  3. // event.detail 的值为当前选中项的索引
  4. this.setData({ active: event.detail });
  5. wx.switchTab({
  6. url: this.data.list[event.detail].pagePath,
  7. })
  8. },
  9. }

问题:在测试中发现选中tabBar需连点两次才有选中效果

解决办法:active进行公共存储

CODE

store.js新增

  1. // 通过es6导出语法 导出store实例对象
  2. export const store=observable({
  3. activeTabBarIndex:0
  4. xxx
  5. // updateActiveTabBarIndex指向一个方法
  6. updateActiveTabBarIndex:action(function (index) {
  7. this.activeTabBarIndex=index
  8. })
  9. })

index.js新增

  1. storeBindings:{
  2. xxx
  3. fields:{
  4. xxx
  5. active:'activeTabBarIndex'
  6. },
  7. // 映射方法
  8. actions:{
  9. updateActive:'updateActiveTabBarIndex'
  10. }
  11. },
  1. methods: {
  2. onChange(event) {
  3. // event.detail 的值为当前选中项的索引
  4. // this.setData({active:event.detail})不需要,更改active值要通过store的方法改
  5. this.updateActive(event.detail),
  6. xxx
  7. },
  8. }

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

闽ICP备14008679号