• vue移动端Tabbar 标签栏(简单)


    目录

    效果图

    实现步骤:

    1.通过 npm 安装

    2.引入组件

    自动按需导入

    3.配置

    4.Tabbar 标签栏使用

    前提准备:

    使用:

     参数说明:


    使用组件:Vant

    Vant 是一个轻量、可靠的移动端组件库

    效果图

    实现步骤:

    1.通过 npm 安装

    在现有项目中使用 Vant 时,可以通过 npmyarn 进行安装:

    # Vue 3 项目,安装最新版 Vant:

    npm i vant -S

    # Vue 2 项目,安装 Vant 2:

    npm i vant@latest-v2 -S

    2.引入组件

    自动按需导入

    Vant 支持一次性导入所有组件,引入所有组件会增加代码包体积,推荐自动按需引入组件

    项目终端执行命令安装插件:

    npm i babel-plugin-import -D

    babel-plugin-import 是一款 babel 插件,它会在编译过程中将 import 的写法自动转换为按需引入的方式。

    3.配置

    第一种:对于使用 babel7 的用户,可以在 babel.config.js 中配置

    1. module.exports = {
    2. plugins: [
    3. ['import', {
    4. libraryName: 'vant',
    5. libraryDirectory: 'es',
    6. style: true
    7. }, 'vant']
    8. ]
    9. };

     第二种:在.babelrc 中添加配置

    1. // 注意:webpack 1 无需设置 libraryDirectory
    2. {
    3. "plugins": [
    4. ["import", {
    5. "libraryName": "vant",
    6. "libraryDirectory": "es",
    7. "style": true
    8. }]
    9. ]
    10. }

    4.Tabbar 标签栏使用

    前提准备:

    在views文件夹下新建几个跳转页面

    在router/index.js配置路由

    1. import Vue from 'vue'
    2. import VueRouter from 'vue-router'
    3. import Home from '../views/home/index.vue'
    4. Vue.use(VueRouter)
    5. const routes = [
    6. {
    7. path: '/',
    8. redirect: "/home"
    9. },
    10. {
    11. path: '/home',
    12. name: 'home',
    13. component: Home
    14. },
    15. {
    16. path: '/classify',
    17. name: 'classify',
    18. component: () => import('../views/classify/index.vue')
    19. },
    20. {
    21. path: '/cart',
    22. name: 'cart',
    23. component: () => import('../views/cart/index.vue')
    24. },
    25. {
    26. path: '/my',
    27. name: 'my',
    28. component: () => import('../views/my/index.vue')
    29. },
    30. ]
    31. const router = new VueRouter({
    32. mode: 'hash',
    33. base: process.env.BASE_URL,
    34. routes
    35. })
    36. export default router

    使用:

    在main.js中全局按需引入:

    1. import { Tabbar, TabbarItem } from 'vant';
    2. Vue.use(Tabbar);
    3. Vue.use(TabbarItem);

    在App.vue中使用:

    1. <template>
    2. <div id="app">
    3. <van-tabbar route active-color="#ee0a24" v-if="isTabShow">
    4. <van-tabbar-item replace to="/home" icon="wap-home-o">首页van-tabbar-item>
    5. <van-tabbar-item replace to="/classify" icon="search">分类van-tabbar-item>
    6. <van-tabbar-item replace to="/cart" icon="shopping-cart-o">购物车van-tabbar-item>
    7. <van-tabbar-item replace to="/my" icon="manager-o">我的van-tabbar-item>
    8. van-tabbar>
    9. <router-view>router-view>
    10. div>
    11. template>
    12. <script>
    13. export default {
    14. data() {
    15. return {
    16. //true为显示tabbar栏,false为隐藏
    17. isTabShow: true
    18. }
    19. },
    20. //监听路由变化,显示或隐藏tabbar栏
    21. watch: {
    22. "$route": function (to) {
    23. var tabArr = ["/home", "/classify", "/cart", "/my"];
    24. if (tabArr.includes(to.path)) {
    25. this.isTabShow = true
    26. } else {
    27. this.isTabShow = false
    28. }
    29. }
    30. },
    31. }
    32. script>

     参数说明:

    active-color:选中时的颜色

    icon:图标,文档,引用图标下方的名字

    replace to:跳转路径,与router/index.js中的path相对应

  • 相关阅读:
    建造者模式
    【记录】算法 - C/C++版
    【无标题】【Koltin Flow(三)】Flow操作符之中间操作符(二)
    学习记录:TIM—电容按键检测
    DHorse(K8S的CICD平台)的实现原理
    [附源码]计算机毕业设计少儿节目智能推荐系统Springboot程序
    基于Microsoft SemanticKernel和GPT4实现一个智能翻译服务
    【学习笔记】空间坐标系旋转与四元数
    在idea下新建javaweb项目-部署-运行
    C语言刷题(Day1)
  • 原文地址:https://blog.csdn.net/qq_52126119/article/details/127834224