Vue.js底部导航三种写法

位置:首页>文章>详情   分类: 教程分享 > Vue.js教程   阅读(4137)   2024-03-16 09:31:02

普通写法

普通写法
 

vantUI 使用字体图标和文字

<template>
    <van-tabbar v-model="active" active-color="#07c160">
        <van-tabbar-item :to="{name:'home'}">
            <span>首页</span>
            <img
            slot="icon"
            slot-scope="props"
            :src="props.active ? icon.homeActive : icon.homeNormal"
            >
        </van-tabbar-item>
        <van-tabbar-item :to="{name:'money'}">
            <span>钱包</span>
            <img
            slot="icon"
            slot-scope="props"
            :src="props.active ? icon.moneyActive : icon.moneyNormal"
            >
        </van-tabbar-item>
        <van-tabbar-item :to="{name:'user'}">
            <span>个人中心</span>
            <img
            slot="icon"
            slot-scope="props"
            :src="props.active ? icon.userActive : icon.userNormal"
            >
        </van-tabbar-item>
    </van-tabbar>
</template>

<script>
export default {
    data() {
        return {
            active: 0,
            icon:{
                homeNormal: require('./home.png'),
                homeActive: require('./homeActive.png'),
                moneyNormal: require('./money.png'),
                moneyActive: require('./moneyActive.png'),
                userNormal: require('./user.png'),
                userActive: require('./userActive.png'),
            },
        }
    }
}
</script>

 

手写 适用于背景图等复杂样式

 

<template>
  <div class="footer">
    <div class="foot">
      <router-link
        v-for="(item,i) in footnavs"
        :key="i"
        :to="item.to"
        :class="{'selected':($route.meta.nav_index==item.nav_index)}"
      >
        <img :src="$route.meta.nav_index==item.nav_index?item.selected:item.no_selected" alt>
        <span v-show="!($route.meta.nav_index==item.nav_index)">{{item.name}}</span>
      </router-link>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      footnavs: [
        {
          to: "/",
          name: "首页",
          no_selected: require("@img/home/shouye@2x.png"),
          selected: require("@img/home/shouye2@2x.png"),
          nav_index: 1
        },
        {
          to: "/money",
          name: "钱包",
          no_selected: require("@img/home/qianbao@2x.png"),
          selected: require("@img/home/qianbao2@2x.png"),
          nav_index: 2
        },
        {
          to: "/my",
          name: "个人中心",
          no_selected: require("@img/home/gerenhzongxin@2x.png"),
          selected: require("@img/home/gerenzhongxin@2x.png"),
          nav_index: 3
        }
      ]
    };
  }
};
</script>   
<style lang="less" scoped>
.footer {
  width: 100%;
  height: 98px;
  margin-top: 12px;
  .foot {
    width: 100%;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: space-between;
    padding: 10px 20px;
    background: rgba(255, 255, 255, 1);
    box-shadow: 0px 1px 16px 0px rgba(192, 192, 192, 0.4);

    a {
      width: 33%;
      text-align: center;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      align-items: center;
      img {
        width: 47px;
        height: 47px;
      }
      span {
        font-size: 28px;
        color: rgba(102, 102, 102, 1);
      }
      &.selected {
        img {
          width: 85px;
          height: 85px;
        }
      }
    }
  }
}
</style>

路由文件

import Vue from 'vue'
import Router from 'vue-router'
import home from '@/pages/home/home'
import noticeList from '@/pages/home/noticeList'
import money from '@/pages/money/money'
import my from '@/pages/my/my'

Vue.use(Router)

export default new Router({
    routes: [{
            path: '/',
            name: 'home',
            component: home,
            meta: {
                index: 0,
                hasFooter: true,
                nav_index: 1
            },
        },
        {
            path: '/money',
            name: 'money',
            component: money,
            meta: {
                index: 0,
                hasFooter: true,
                nav_index: 2
            },
        },
        {
            path: '/my',
            name: 'my',
            component: my,
            meta: {
                index: 0,
                hasFooter: true,
                nav_index: 3
            },
        },
        {
            path: '/noticeList',
            name: 'noticeList',
            component: noticeList,
            meta: {
                index: 1,
                hasBar: true,
            },
        },

    ],
    //该方法用于控制滚动行为,
    scrollBehavior(to, form, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            return { x: 0, y: 0 }
        }
    }
})

app.vue文件

<template>
  <div id="app">
    <tabBar v-show="$route.meta.hasBar"/>
    <transition :name="transitionName">
      <router-view/>
    </transition>
    <Footer v-show="$route.meta.hasFooter"/>
  </div>
</template>

<script>
import "./common/css/common.css";
// bug:不能为小写footer会报错
import Footer from "@/components/footer";
import tabBar from "@/components/tabBar";
export default {
  name: "App",
  data() {
    return {
      transitionName: ""
    };
  },
  components: {
    Footer,
    tabBar
  },
  mounted() {},
  watch: {
    /*
     * 使用watch 监听$router的变化
     * 如果to索引大于from索引,判断为前进状态,反之则为后退状态
     * 设置动画名称
     */
    $route(to, from) {
      if (to.meta.index == from.meta.index) {
        this.transitionName = "";
      } else if (to.meta.index > from.meta.index) {
        this.transitionName = "slide-left";
      } else {
        this.transitionName = "slide-right";
      }
    }
  }
};
</script>
标签: Vue导航 vue
地址:https://www.leftso.com/article/624.html

相关阅读

vue-router 中 routers 定义写法,讨论 require 的使用与否​首先上 routerindex.jsimport Vue from 'vue'import Router f...
普通写法​vantUI 使用字体图标和文字&lt;template&gtl; &lt;van-tabbar v-model="active" active-color="#07c160"&gtl...
在初始化的Vue项目中,我们最先接触到的就是main.js,App.vue,index.html这三个文件,从下面创建的一个空白项目中可以看到:​关于三个文件的说明如下:index.html---主页
vue
Vue v-if判断数组长度 searchResultDataList为vue定义的data里面的变量&lt;li  v-if="Object.keys(searchResultDataLis...
vue打包会把vue相关的组件打包到一个文件vendor.*.js(*是个随机数)步骤一 资源引入vue最外层index.html文件引入资源文&lt;body&gtl;     &lt;di...
Vue 如何返回上一页(上一个锚点)//...省略 methods:{ goback:function(){ this.$router.go(-1);//...
vue
在初始化完一个vue项目(基于vue-cli 和webpack)之后,我们可以通过 npm run dev来让这个项目跑起来
1.vue项目中创建global.js创建global.js放于main.js同级目录(可自己随意放)export default {   getToken()   {     retur...
vue watch监控对象属性变化watch:{ 'object.attr':function attr(value){ //处理操作 } ...
一、前言Vue 2.x 使用期间,我们会创建众多组件,这里我们将讨论一下各个组件直接的相互通讯问题如何解决