[项目篇]vue3+ts 封装底部tabbar - 第三天

简介: [项目篇]vue3+ts 封装底部tabbar - 第三天

效果图



网络异常,图片无法展示
|


查看文档



vant的tabbar标签栏


网络异常,图片无法展示
|


俺们选这个,带自定义图标的


网络异常,图片无法展示
|


1. 先写骨架


<template>
  <van-tabbar v-model="active" fixed route active-color=”#6689e2“>
    <van-tabbar-item v-for="(item, index) in tabbars" :to="item.to" :key="index">
        <span>{{ item.title }}</span>
        <template #icon="props">
            <img :src.sync="props.active ? item.image_active : item.image" />
      </template>
    </van-tabbar-item>
  </van-tabbar>
</template>
复制代码


2. 写js,由于是做成组件,所以需要单独抽离出来


<script lang="ts">
import { defineComponent, PropType, ref } from "vue"
// ts 定义类型并且暴露出去
export interface ITabList {
  title: string          // 标题
  to: { name: string }   // url路径
  image: string          // 初始图标
  image_active: string   // 选中图标
}
export default defineComponent({
  name: "TabBar",
  props: {
    defaultActive: {
      type: Number,
      default: 0
    },
    tabbars: {
      type: Array as PropType<ITabList[]>, // 类型断言
      default: () => {
        return []
      }
    }
  },
  setup(props) {
    const active = ref(props.defaultActive)
    return { active }
  }
})
</script>
复制代码


至此,tabbar就已经完成了。

让我们试验一下吧


views里面,创建layout.vue


<!-- layouts -->
<template>
  <div class="app-container">
    <div class="layout-content">
      <keep-alive v-if="$route.meta.keepAlive">
        <router-view></router-view>
      </keep-alive>
      <router-view v-else></router-view>
    </div>
    <div class="layout-footer" v-if="$route.meta.showTab">
      <!-- 这里@change默认绑定在了van-tabbar上,参考vue的$attr -->
      <TabBar
        :tabbars="tabbars"
        :defaultActive="defaultActive"
        @change="handleChange"
      />
    </div>
  </div>
</template>
<script lang="ts">
import { computed, defineComponent, reactive, toRefs } from "vue"
import TabBar, { ITabList } from "@/components/TabBar.vue"
import { useRoute } from "vue-router"
import indexPng from '@assets/tabbar/index.png'
import indexActivePng from '@assets/tabbar/index-active.png'
import etcPng from '@assets/tabbar/etc.png'
import etcActivePng from '@/assets/tabbar/etc-active.png'
import bbsPng from '@assets/tabbar/bbs.png'
import bbsActivePng from '@assets/tabbar/bbs-active.png'
import couponPng from '@assets/tabbar/coupon.png'
import couponActivePng from '@assets/tabbar/coupon-active.png'
import myPng from '@assets/tabbar/my.png'
import myActivePng from '@assets/tabbar/my-active.png'
interface ILayoutState {
  tabbars: Array<ITabList>
  defaultActive: number
}
export default defineComponent({
  name: "layouts",
  components: { TabBar },
  setup() {
    const route = useRoute();
    const state: ILayoutState = reactive({
      tabbars: [
        { title: "首页", to: { name: "Index" }, image: indexPng, image_active: indexActivePng },
        { title: "记账", to: { name: "Etc" },   image: etcPng,   image_active: etcActivePng },
        { title: "论坛", to: { name: "Todo" },  image: bbsPng,   image_active: bbsActivePng },
        { title: "优惠", to: { name: "Coupon" },image: couponPng,image_active: couponActivePng },
        { title: "我的", to: { name: "My" },    image: myPng,    image_active: myActivePng }
      ],
      defaultActive: computed(() => {
        return state.tabbars.findIndex((item: ITabList) => {
          return item.to.name === route.name
        })
      })
    })
    const handleChange = (v: number) => {
      console.log("tab value:", v)
    }
    return {
      ...toRefs(state),
      handleChange
    }
  }
});
</script>
复制代码


这里有一个坑,不知道为什么,我直接写'@/assets/tabbar/index.png',这种格式会报错。。所以就只能用import * from ** 的方式来实现了。有小伙伴有答案麻烦告诉我一下,谢谢


ok,功能实现了,那么就该去定义router和创建文件了

  1. views里面,创建tabbar文件夹,并且创建五个对应tabbar的文件


网络异常,图片无法展示
|


  1. 然后,去router文件夹下面的router.config.js里面写对应的路由

import { RouteRecordRaw } from "vue-router";
export const constantRouterMap: Array<RouteRecordRaw> = [
  {
    path: "/",
    name: "/",
    component: () => import("@/views/layout.vue"),
    redirect: "/Index",
    meta: { title: "首页", keepAlive: false },
    children: [
      { path: "/index", name: "Index", component: () => import("@/views/tabbar/index.vue"), meta: { title: "首页", keepAlive: false, showTab: true } },
      { path: "/etc", name: "Etc", component: () => import("@/views/tabbar/etc.vue"), meta: { title: "记账", keepAlive: false, showTab: true } },
      { path: "/todo", name: "Todo", component: () => import("@/views/tabbar/todo.vue"), meta: { title: "论坛", keepAlive: false, showTab: true } },
      { path: "/coupon", name: "Coupon", component: () => import("@/views/tabbar/coupon.vue"), meta: { title: "优惠", keepAlive: false, showTab: true } },
      { path: "/my", name: "My", component: () => import("@/views/tabbar/my.vue"), meta: { title: "我的", keepAlive: false, showTab: true } }
    ]
  }
]
复制代码


酱紫,一个完整的tabbar路由封装就实现啦!!!

相关文章
|
2月前
|
JavaScript 前端开发 安全
Vue 3
Vue 3以组合式API、Proxy响应式系统和全面TypeScript支持,重构前端开发范式。性能优化与生态协同并进,兼顾易用性与工程化,引领Web开发迈向高效、可维护的新纪元。(238字)
511 139
|
2月前
|
缓存 JavaScript 算法
Vue 3性能优化
Vue 3 通过 Proxy 和编译优化提升性能,但仍需遵循最佳实践。合理使用 v-if、key、computed,避免深度监听,利用懒加载与虚拟列表,结合打包优化,方可充分发挥其性能优势。(239字)
227 1
|
3月前
|
开发工具 iOS开发 MacOS
基于Vite7.1+Vue3+Pinia3+ArcoDesign网页版webos后台模板
最新版研发vite7+vue3.5+pinia3+arco-design仿macos/windows风格网页版OS系统Vite-Vue3-WebOS。
392 11
|
2月前
|
JavaScript 安全
vue3使用ts传参教程
Vue 3结合TypeScript实现组件传参,提升类型安全与开发效率。涵盖Props、Emits、v-model双向绑定及useAttrs透传属性,建议明确声明类型,保障代码质量。
263 0
|
4月前
|
缓存 前端开发 大数据
虚拟列表在Vue3中的具体应用场景有哪些?
虚拟列表在 Vue3 中通过仅渲染可视区域内容,显著提升大数据列表性能,适用于 ERP 表格、聊天界面、社交媒体、阅读器、日历及树形结构等场景,结合 `vue-virtual-scroller` 等工具可实现高效滚动与交互体验。
446 1
|
JavaScript Java 物联网
现有vue项目seo优化
现有vue项目seo优化
|
JavaScript 前端开发
重读vue电商网站45之项目优化上线
重读vue电商网站45之项目优化上线
207 0
重读vue电商网站45之项目优化上线
|
3月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
311 2
|
2月前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
295 137
|
6月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
798 0