1+ <!-- src/App.vue -->
12<template >
2- <div
3- :class =" [isDarkMode ? 'dark' : '']"
4- :style =" themeStyle"
5- class =" min-h-screen font-sans transition-colors duration-300 bg-slate-50 dark:bg-slate-900 text-slate-700 dark:text-slate-200"
6- >
7- <!-- 顶部导航 -->
8- <NavBar />
9- <div class =" container-main dark:bg-gray-900 dark:from-gray-900 dark:to-gray-800 min-h-screen" >
10- <transition name =" fade-slide" mode =" out-in" >
11- <RouterView />
12- </transition >
13- </div >
14- </div >
15- <Footer />
3+ <!-- 动态渲染当前路由对应的布局 -->
4+ <component :is =" currentLayout" />
165</template >
176
187<script setup lang="ts">
19- import NavBar from ' ./components/NavBar.vue'
20- import Footer from ' ./components/Footer.vue' ;
21- import HomePage from ' ./views/HomePage.vue'
8+ import { computed } from ' vue' ;
9+ import { useRoute } from ' vue-router' ;
10+ // 导入所有布局组件
11+ import MainLayout from ' @/layouts/MainLayout.vue' ;
12+ import BlankLayout from ' @/layouts/BlankLayout.vue' ;
2213import { provideTheme } from ' @/composables/useTheme' ;
2314// 🚨 关键:确保调用了 provideTheme()
2415const {
2516 isDarkMode,
2617 currentTheme,
2718 themeStyle,
2819 themeColors,
20+ setMode,
2921 setTheme
3022} = provideTheme ();
31- // 这行代码将主题状态注入到整个应用树中,供所有子组件使用。
32- </script >
3323
34- <style >
24+ const route = useRoute ();
25+
26+ // 布局映射:路由 meta.layout 对应实际布局组件
27+ const layoutMap = {
28+ main: MainLayout , // 主布局(默认)
29+ blank: BlankLayout // 空白布局
30+ };
3531
36- .container-main {
37- max-width : 100vw ;
38- width : 100% ;
39- }
40- /* 进入时动画 */
41- .fade-slide-enter-active {
42- transition : opacity 0.5s ease , transform 0.5s ease ;
43- }
44- .fade-slide-enter-from {
45- opacity : 0 ;
46- transform : translateY (20px );
47- }
48- /* 离开时动画 */
49- .fade-slide-leave-active {
50- transition : opacity 0.3s ease , transform 0.3s ease ;
51- }
52- .fade-slide-leave-to {
53- opacity : 0 ;
54- transform : translateY (-20px );
55- }
56- </style >
32+ // 计算当前要渲染的布局(默认使用主布局)
33+ const currentLayout = computed (() => {
34+ // 从路由 meta 中获取布局类型,未指定则默认主布局
35+ const layoutType = route .meta .layout as ' main' | ' blank' || undefined ;
36+ return layoutMap [layoutType ] || MainLayout ;
37+ });
38+
39+ </script >
5740
41+ <!-- 全局样式可移到 src/styles/main.scss,这里清空 -->
42+ <style ></style >
0 commit comments