Skip to content

Commit

Permalink
feat: Layout 布局
Browse files Browse the repository at this point in the history
  • Loading branch information
pany-ang committed Feb 13, 2025
1 parent f7b9b17 commit d7a55f8
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<script setup lang="ts">
import Layout from "@/layout/index.vue"
</script>

<template>
<router-view />
<Layout />
</template>
20 changes: 20 additions & 0 deletions src/layout/components/NavBar.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<script setup lang="ts">
const route = useRoute()
const title = computed(() => route.meta.title)
const showLeftArrow = computed(() => route.meta.layout?.navBar?.showLeftArrow)
function onClickLeft() {
history.back()
}
</script>

<template>
<van-nav-bar
:title="title"
:left-arrow="showLeftArrow"
@click-left="onClickLeft"
safe-area-inset-top placeholder fixed
/>
</template>
26 changes: 26 additions & 0 deletions src/layout/components/Tabbar.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<script setup lang="ts">
const router = useRouter()
const tabbarItemList = computed(() => {
const routes = router.getRoutes()
return routes.filter(route => route.meta.layout?.tabbar?.showTabbar).map(route => ({
title: route.meta.title,
icon: route.meta.layout?.tabbar?.icon,
path: route.path
}))
})
</script>

<template>
<van-tabbar placeholder route safe-area-inset-bottom fixed>
<van-tabbar-item
v-for="item in tabbarItemList"
:key="item.path"
:icon="item.icon"
:to="item.path"
replace
>
{{ item.title }}
</van-tabbar-item>
</van-tabbar>
</template>
18 changes: 18 additions & 0 deletions src/layout/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script setup lang="ts">
import NavBar from "./components/NavBar.vue"
import Tabbar from "./components/Tabbar.vue"
const route = useRoute()
const showNavBar = computed(() => route.meta.layout?.navBar?.showNavBar)
const showTabbar = computed(() => route.meta.layout?.tabbar?.showTabbar)
</script>

<template>
<NavBar v-if="showNavBar" />
<router-view overflow-y-auto />
<Tabbar v-if="showTabbar" />
</template>

<style scoped></style>
12 changes: 11 additions & 1 deletion src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ export const routes: RouteRecordRaw[] = [
component: () => import("@/pages/index.vue"),
name: "Index",
meta: {
title: "首页"
title: "首页",
layout: {
navBar: {
showNavBar: true,
showLeftArrow: false
},
tabbar: {
showTabbar: true,
icon: "home-o"
}
}
}
}
]
Expand Down
4 changes: 4 additions & 0 deletions types/auto/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ declare module 'vue' {
export interface GlobalComponents {
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
VanIcon: typeof import('vant/es')['Icon']
VanNavBar: typeof import('vant/es')['NavBar']
VanTabbar: typeof import('vant/es')['Tabbar']
VanTabbarItem: typeof import('vant/es')['TabbarItem']
}
}
34 changes: 34 additions & 0 deletions types/vue-router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,40 @@ export {}

declare module "vue-router" {
interface RouteMeta {
/**
* @description 页面标题
*/
title?: string
/**
* @description 布局配置
*/
layout?: {
/**
* @description NavBar 配置
*/
navBar?: {
/**
* @description 是否显示 NavBar
* @default false
*/
showNavBar?: boolean
/**
* @description 是否显示左侧箭头
* @default false
*/
showLeftArrow?: boolean
}
tabbar?: {
/**
* @description 是否显示 Tabbar
* @default false
*/
showTabbar?: boolean
/**
* @description 图标
*/
icon?: string
}
}
}
}

0 comments on commit d7a55f8

Please sign in to comment.