《Vux踩坑记》 之 Tabbar

《Vux踩坑记》 之 Tabbar

思路

总共4个页面,每个页面的底部有tabbar,分别为首页、监控、告警、我的,通过点击不同item进入不同页面

要求

  • 点击跳转后,tabbar选中相应的item。

决定:

  • 通过mutations去跳转

代码

封装Tabbar.vue

<template>
  <div>
    <tabbar v-model="tabbarIndex" style="position: fixed">
      <tabbar-item @on-item-click="gotoTabbarPage(0)">
        <img slot="icon" src="../../assets/home/homepage.png">
        <img slot="icon-active" src="../../assets/home/homepage_fill.png">
        <span slot="label">首页</span>
      </tabbar-item>
      <tabbar-item @on-item-click="gotoTabbarPage(1)">
        <img slot="icon" src="../../assets/home/dynamic.png">
        <img slot="icon-active" src="../../assets/home/dynamic_fill.png">
        <span slot="label">监控</span>
      </tabbar-item>
      <tabbar-item @on-item-click="gotoTabbarPage(2)">
        <img slot="icon" src="../../assets/home/remind.png">
        <img slot="icon-active" src="../../assets/home/remind_fill.png">
        <span slot="label">告警</span>
      </tabbar-item>
      <tabbar-item @on-item-click="gotoTabbarPage(3)">
        <img slot="icon" src="../../assets/home/people.png">
        <img slot="icon-active" src="../../assets/home/people_fill.png">
        <span slot="label">我的</span>
      </tabbar-item>
    </tabbar>
  </div>
</template>

<script>
  import { Tabbar, TabbarItem, Group, Cell } from 'vux'

  export default {
    name: 'MyTabbar',
    components: {
      Tabbar,
      TabbarItem,
      Group,
      Cell
    },
    data () {
      return {
        // 从state中获取全局tabbarIndex
        tabbarIndex: this.$store.state.tabbarIndex
      }
    },
    methods: {
      gotoTabbarPage (tabbarIndex) {
        // 由于这里需要更改全局tabbarIndex,所以必须通过mutations方式去跳转
        // 更新底部菜单index
        this.$store.dispatch('updateTabbarIndex', {tabbarIndex: tabbarIndex})
      }
    }
  }
</script>

关键点

v-model="tabbarIndex",内置方法,会选中索引等于tabbarIndex的item, 其中tabbarIndex的值可以通过$store获取

data () {
      return {
        // 从state中获取全局tabbarIndex
        tabbarIndex: this.$store.state.tabbarIndex
      }
}

actions.js

import * as types from './mutation-types.js'

// 更新全局tabbar索引
export const updateTabbarIndex = ({commit}, param) => {
  commit(types.UPDATE_TABBAR_INDEX, {
    tabbarIndex: param.tabbarIndex
  })
}

mutation-types.js

export const UPDATE_TABBAR_INDEX = 'UPDATE_TABBAR_INDEX'

mutations.js

import * as types from './mutation-types.js'
import VueRouter from '../router/routes.js'

export default {
  [types.UPDATE_TABBAR_INDEX] (state, {tabbarIndex}) {
    console.log('执行UPDATE_TABBAR_INDEX,tabbarIndex=' + tabbarIndex)
    // 改变值
    state.tabbarIndex = tabbarIndex
    if (tabbarIndex === 0) {
      VueRouter.push({
        path: '/home'
      })
    }
    if (tabbarIndex === 3) {
      VueRouter.push({
        path: '/account'
      })
    }
  }
}

state.js

export default {
  tabbarIndex: 0
}

关于其余相关代码就不赘述了,有兴趣的前往查看源码 https://gitee.com/gmarshal/foruo-learn-vue-h5 请选择V0.1版本

博客

https://my.oschina.net/gmarshal/blog/1827973

转载于:https://my.oschina.net/gmarshal/blog/1827973

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值