Vue实现底部导航栏切换页面及图片

前言

刚进新公司,有幸接触到从前后端不分离到前后端分离的一个过程,最开始对vue不太熟悉,下班自学一周就开始做了,可能会有很多问题,若有写不好的地方大佬们可以提出。

一、实现效果

需求:vue底部导航点击切换图标
效果:
在这里插入图片描述
在这里插入图片描述

二、大概思路图

在这里插入图片描述

三、代码实现

index.js(vuex)

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    // 目录标识(感觉不应该使用 localStorage,应该使用sessionStorage)
    DirectoryIdentifier: localStorage.getItem('DirectoryIdentifier') ? localStorage.getItem('DirectoryIdentifier') : ''
  },
  mutations: {
    // 保存目录标识
    saveDirectoryIdentifier (state, directoryIdentifier) {
      localStorage.setItem('DirectoryIdentifier', directoryIdentifier)
      state.DirectoryIdentifier = directoryIdentifier
    }
  }
})
export default store

login.vue

methods: {
    login () {
      if (this.loginForm.username === '' || this.loginForm.tmpPassword === '') {
        this.showPopup('账号或密码不能为空')
      } else {
        api.login(this.loginForm).then((data) => {
          console.log(data)
          if (data.code === '1') {
            // 保存目录标识(device)
            this.$store.commit('saveDirectoryIdentifier', 'device')
            this.$router.push('/home/device')
          } else {
            this.showPopup(data.message)
          }
        })
      }
    }

index.vue

<template>
  <div class = "main">
    <div><router-view></router-view></div>
    <ul class="nav-bottom">
      <li @click="directoryIdentifierClick('device')" v-if="isShowDevice">
          <router-link to="/home/device">
          <div>
            <span class="nav-bottom-span nav-img"><img :src='deviceImgUrl'></span>
            <span class="nav-bottom-span">名称</span>
          </div>
          </router-link>
      </li>
      <li @click="directoryIdentifierClick('deviceSensor')" v-if="isShowDeviceSensor">
        <router-link to="/home/device_sensor">
        <div>
          <span class="nav-bottom-span nav-img"><img :src='deviceSensoImgUrl'></span>
          <span class="nav-bottom-span">名称</span>
        </div>
        </router-link>
      </li>
      <li @click="directoryIdentifierClick('hospital')" v-if="isShowHospital">
        <router-link to="hospital" >
         <div>
          <span class="nav-bottom-span nav-img"><img :src='hospitalImgUrl'></span>
          <span class="nav-bottom-span">名称</span>
         </div>
        </router-link>
      </li>
      <li @click="directoryIdentifierClick('doctorReport')" v-if="isShowdDoctorReport">
        <router-link to="/home/doctor_report">
          <div>
          <span class="nav-bottom-span nav-img"><img :src='doctorReportImgUrl'></span>
          <span class="nav-bottom-span">名称</span>
          </div>
        </router-link>
      </li>
      <li @click="directoryIdentifierClick('user')" v-if="isShowUser">
        <router-link to="/home/user">
          <div>
          <span class="nav-bottom-span nav-img"><img :src='userImgUrl'></span>
          <span class="nav-bottom-span">名称</span>
          </div>
        </router-link>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      deviceImgUrl: '../../../static/img/device-nav-out.png',
      deviceSensoImgUrl: '../../../static/img/device-sensor-out.png',
      hospitalImgUrl: '../../../static/img/hospital-nav-out.png',
      doctorReportImgUrl: '../../../static/img/prescription-nav-out.png',
      userImgUrl: '../../../static/img/user-out.png',
      isShowDevice: false,
      isShowDeviceSensor: false,
      isShowHospital: false,
      isShowdDoctorReport: false,
      isShowUser: false
    }
  },
  // 钩子事件(触发机制:初始化) 刷新标识定位问题
  mounted: function () {
    // 这个是获取权限,如果没有可以去掉
    this.getWechatChanelAcl()
    // mounted - 初始化定位标识
    this.initIdentifier()
  },
  // 钩子事件(触发机制:数据修改时)
  beforeUpdate: function () {
    // 这个是获取权限,如果没有可以去掉
    this.getWechatChanelAcl()
  },
  methods: {
    directoryIdentifierClick (directory) {
      this.setGrayIdentifier(directory)
      this.$store.commit('saveDirectoryIdentifier', directory)
    },
    // 设置灰色图片
    setGrayIdentifier (directory) {
      this.deviceImgUrl = (directory === 'device' ? '../../../static/img/device-nav-active.png' : '../../../static/img/device-nav-out.png')
      this.deviceSensoImgUrl = (directory === 'deviceSensor' ? '../../../static/img/device-sensor-active.png' : '../../../static/img/device-sensor-out.png')
      this.hospitalImgUrl = (directory === 'hospital' ? '../../../static/img/hospital-nav-active.png' : '../../../static/img/hospital-nav-out.png')
      this.doctorReportImgUrl = (directory === 'doctorReport' ? '../../../static/img/prescription-nav-active.png' : '../../../static/img/prescription-nav-out.png')
      this.userImgUrl = (directory === 'user' ? '../../../static/img/user-active.png' : '../../../static/img/user-out.png')
    },
     // mounted - 初始化定位标识
    initIdentifier () {
      let directoryIdentifier = this.$store.state.DirectoryIdentifier
      if (directoryIdentifier === null) {
        this.deviceImgUrl = '../../../static/img/device-nav-active.png'
      } else {
        this.setGrayIdentifier(directoryIdentifier)
      }
    },
    // mounted-beforeUpdate 获取目录资源权限
    getWechatChanelAcl () {
      let wechatChanelAcl = JSON.parse(this.$store.state.WechatChanelAcl)
      for (let index = 0; index < wechatChanelAcl.length; index++) {
        const chanelAcl = wechatChanelAcl[index]
        if (chanelAcl.permission === 'wecharsalechanel:rp_device:list') {
          this.isShowDevice = true
        } else if (chanelAcl.permission === 'wecharsalechanel:device_sensor:list') {
          this.isShowDeviceSensor = true
        } else if (chanelAcl.permission === 'wecharsalechanel:hospital:list') {
          this.isShowHospital = true
        } else if (chanelAcl.permission === 'wecharsalechanel:doctor_report:list') {
          this.isShowdDoctorReport = true
        } else if (chanelAcl.permission === 'wecharsalechanel:user:list') {
          this.isShowUser = true
        }
      }
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.main {
  background: #eee;
}
.nav-bottom {
    padding-top: 0;
    width: 100%;
    position: fixed;
    bottom: 0;
    border-top: 1px solid #eee;
    color: gray;
    background: #fff;
    height: 4rem;
}
.nav-bottom>li {
    float: left;
    width: 20%;
    text-align: center;
    box-sizing: border-box;
    height: 4rem;
}
.nav-bottom-span {
    font-size: 14px;
    display: block;
    line-height: 0.6rem;
    padding-top: 0.6rem;
}
.nav-img img {
    height: 1.3rem;
}
</style>

测试

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值