0.0.9 • Published 5 years ago

mpvue-tab v0.0.9

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

mpvue自定义微信小程序tabbar

  • 可以给tabBar添加双击事件
  • 可以给tabBar添加长按事件
  • 在子页面定义tabBar

效果演示

image

使用文档

  • 安装
npm i mpvue-tab
  • mpvue项目中使用
<template>
  <div class="container">
    <!-- <component :is="currentTabComponent"></component> -->
    <!-- 通过v-if切换页面 -->
    <home v-if="currentPage===0" />
    <job v-if="currentPage===1" />
    <me v-if="currentPage===2" />
    <mpvue-tabbar
      :list="list"
      position="bottom"
      fontSize="30rpx"
      color="#606266"
      selectedColor="#3794ff"
      :index = "currentPage"
      @switchTab="onSwitchTab"
      @doubleClick="onDoubleClick"
      @longpress="onLongPress"
    />
  </div>
</template>

<script>
import me from '@/pages/me/index'
import home from '@/pages/home/index'
import job from '@/pages/job/index'
import mpvueTabbar from 'mpvue-tabbar'
export default {
  data () {
    return {
      currentPage: 2,
      list: [{
        text: '首页',
        iconPath: '/static/home.png',
        selectedIconPath: '/static/homeSelected.png',
        navigationBarTitle: '首页',
        navigationBarFrontColor: '#ffffff',
        navigationBarBackgroundColor: '#000000',
        dot: true,
        dotColor: '#3794ff',
        badgeText: 1
      }, {
        text: '岗位',
        iconPath: '/static/job.png',
        selectedIconPath: '/static/jobSelected.png'
      }, {
        text: '我的',
        iconPath: '/static/me.png',
        selectedIconPath: '/static/meSelected.png',
        navigationBarTitle: '我的'
      }]
    }
  },
  components: {
    mpvueTabbar,
    job,
    home,
    me
  },
  methods: {
    // 切换tab
    onSwitchTab (pageIndex) {
      this.currentPage = pageIndex
      console.log('切换tab', pageIndex)
      wx.showToast({
        title: '切换tab'
      })
    },
    // 双击事件
    onDoubleClick (pageIndex) {
      this.currentPage = pageIndex
      console.log('双击', pageIndex)
      wx.showToast({
        title: '双击'
      })
    },
    // 长按tab事件
    onLongPress (pageIndex) {
      this.currentPage = pageIndex
      console.log('长按', pageIndex)
      wx.showToast({
        title: '长按'
      })
    }
  },

  computed: {
  },

  onShow () {
    // 隐藏小程序自带的tabBar
    wx.hideTabBar({})
  }
}
</script>

<style scoped>
@import url("~mpvue-tabbar/src/mpvue-tabbar.css");
.container {
  padding: 20rpx;
  color: #606266;
  text-align: left;
  background: #fff;
}
</style>
  • 属性
名称类型默认值描述
listArray[]tabBar数据设置列表
positionStringbottom设置tabBar位置,bottom:在底部; top: 在顶部
fontSizeString30rpx自定义字体大小
colorString#606266字体颜色
selectedColorString#3794ff当前选中tab字体选择
indexNumber0初始化页面,默认是0
  • list数据项

数组list至少要包含一个tab数据配置对象。

  list: [{
    text: '首页',
    iconPath: '/static/home.png',
    selectedIconPath: '/static/homeSelected.png',
    navigationBarTitle: '首页',
    navigationBarFrontColor: '#ffffff',
    navigationBarBackgroundColor: '#000000',
    dot: true,
    dotColor: '#3794ff',
    badgeText: 1
  }, {
    text: '岗位',
    iconPath: '/static/job.png',
    selectedIconPath: '/static/jobSelected.png'
  }, {
    text: '我的',
    iconPath: '/static/me.png',
    selectedIconPath: '/static/meSelected.png',
    navigationBarTitle: '我的'
  }]

tab数据对象属性含义:

名称类型默认值描述
textString-tab文字
iconPathString-tab的icon位置
selectedIconPathString-被选中的tab的icon位置
navigationBarTitleString默认和text相同头部导航栏的标题
navigationBarFrontColorString#000000头部导航栏的前景颜色值,包括按钮、标题、状态栏的颜色,仅支持 #ffffff 和 #000000, #000等缩写形式或其他值都无效
navigationBarBackgroundColorString#ffffff头部导航栏的背景颜色值,有效值为十六进制颜色,其他无效
badgeTextNumber0在tab右上角显示数字,默认0不显示,可以用作新消息提示
badgeBackgroundColorStringred在tab右上角显示数字的背景颜色值
badgeBackgroundColorStringred在tab右上角显示数字的背景颜色值
badgeTextColorString#fff在tab右上角显示数字的字体颜色值
dotBooleanfalse在tab右上角显示一个点,如果设置badgeText大于0,此属性会失效
dotColorStringred在tab右上角显示小点的颜色值
  • 事件
名称参数描述
switchTabpageIndex:即将打开的新页面索引点击tab触发事件
doubleClickpageIndex:双击的tab所在页面索引双击tab触发事件,两次点击间隔在300ms内才是双击
longpresspageIndex:长按tab的页面索引长按tab 350ms以上触发

注意事项

  1. 使用创建动态组件无效,好像mpvue不支持这个
  2. 通过类似const tabComponent = { template: "" }的对象定义组件也无效,需要从其他文件引入组件才行,好像也是mpvue的问题