版博士V2.0程序
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. # vite-plugin-vue-meta-layouts
  2. `vite` 的 `vue-router` 的元信息布局系统
  3. <br />
  4. ## README 🦉
  5. [English](./README_EN.md) | Chinese
  6. <br />
  7. ## 动机 🤔
  8. [vite-plugin-vue-layouts](https://github.com/JohnCampionJr/vite-plugin-vue-layouts) 的重写版本,在最新版本的 `vite` 中有合理的 `hmr` !!
  9. <br />
  10. <br />
  11. ## 使用 🦖
  12. ### 基础
  13. #### 安装
  14. ```shell
  15. npm i vite-plugin-vue-meta-layouts -D
  16. ```
  17. ```ts
  18. // vite.config.js
  19. import { defineConfig } from 'vite'
  20. import Vue from '@vitejs/plugin-vue'
  21. import MetaLayouts from 'vite-plugin-vue-meta-layouts'
  22. export default defineConfig({
  23. plugins: [Vue(), MetaLayouts()]
  24. })
  25. ```
  26. #### 使用
  27. ```ts
  28. import { setupLayouts } from 'virtual:meta-layouts'
  29. import { createRouter, createWebHistory } from 'vue-router'
  30. const routes = setupLayouts([
  31. {
  32. // ... 页面路由配置
  33. }
  34. ])
  35. const router = createRouter({
  36. routes,
  37. history: createWebHistory()
  38. })
  39. ```
  40. 1. 创建 `layouts/default.vue` 默认布局,此时页面都会被应用该布局
  41. ```html
  42. <template>
  43. default
  44. <router-view />
  45. <!-- 视图出口 -->
  46. </template>
  47. ```
  48. 2. 当然你可以配置不同的的布局
  49. 例如创建 `layouts/other.vue`
  50. ```ts
  51. // 应用 layouts/default.vue 布局
  52. const home = {
  53. path: '/',
  54. component: () => import('./pages/home.vue')
  55. }
  56. // 应用 layouts/other.vue 布局
  57. const about = {
  58. path: '/about',
  59. component: () => import('./pages/home.vue'),
  60. meta: {
  61. layout: 'other' // 通过元信息来管理布局
  62. }
  63. }
  64. const routes = setupLayouts([home, about])
  65. ```
  66. <br />
  67. ### 搭配文件路由
  68. 当然也支持文件路由哦 🤗
  69. #### [vite-plugin-pages](https://github.com/hannoeru/vite-plugin-pages)
  70. ##### 安装
  71. ```shell
  72. npm i vite-plugin-pages -D
  73. ```
  74. ```ts
  75. // vite.config.js
  76. import { defineConfig } from 'vite'
  77. import Vue from '@vitejs/plugin-vue'
  78. import Pages from 'vite-plugin-pages' // 引入文件路由插件
  79. import MetaLayouts from 'vite-plugin-vue-meta-layouts'
  80. export default defineConfig({
  81. plugins: [
  82. Vue(),
  83. Pages(), // 配置文件路由插件
  84. MetaLayouts()
  85. ]
  86. })
  87. ```
  88. ##### 使用
  89. ```ts
  90. import fileRoutes from '~pages' // 引入文件路由表
  91. import { setupLayouts } from 'virtual:meta-layouts'
  92. import { createRouter, createWebHistory } from 'vue-router'
  93. const router = createRouter({
  94. routes: setupLayouts(fileRoutes), // 注册文件路由表
  95. history: createWebHistory()
  96. })
  97. ```
  98. 此时可以通过页面中的自定义块 `route` 的 `meta` 来做布局配置
  99. ```html
  100. <!-- 你的页面 -->
  101. <template> 内容 </template>
  102. <route> { meta: { layout: 'other' } } </route>
  103. ```
  104. <br />
  105. <br />
  106. #### [unplugin-vue-router](https://github.com/posva/unplugin-vue-router)
  107. ##### 安装
  108. ```shell
  109. npm i unplugin-vue-router -D
  110. ```
  111. ##### 使用
  112. ```ts
  113. import { routes } from "vue-router/auto/routes"; // 引入文件路由表
  114. import { setupLayouts } from "virtual:meta-layouts";
  115. import { createRouter, createWebHistory } from "vue-router";
  116. const router = createRouter({
  117. routes: setupLayouts(routes), // 注册文件路由表
  118. history: createWebHistory(),
  119. });
  120. ```
  121. <br />
  122. <br />
  123. ### 配置
  124. ```ts
  125. // vite.config.js
  126. import { defineConfig } from 'vite'
  127. import Vue from '@vitejs/plugin-vue'
  128. import MetaLayouts from 'vite-plugin-vue-meta-layouts'
  129. export default defineConfig({
  130. plugins: [
  131. Vue(),
  132. MetaLayouts({
  133. target: 'src/layouts', // 布局目录,默认 src/layouts
  134. defaultLayout: 'default', // 默认布局,默认为 default
  135. importMode: 'sync' // 加载模式,支持 sync 和 async。默认为自动处理,SSG 时为 sync,非 SSG 时为 async
  136. })
  137. ]
  138. })
  139. ```
  140. <br />
  141. <br />
  142. ### 类型声明 🦕
  143. 如果你是 `ts` 项目,还可以在 `tsconfig.json` 中配置以下声明
  144. ```json
  145. {
  146. "compilerOptions": {
  147. "types": ["vite-plugin-vue-meta-layouts/client"]
  148. }
  149. }
  150. ```
  151. <br />
  152. <br />
  153. ### 注意
  154. 由于布局系统需要在最外层嵌套一层布局路由,所以可能会造成路由表的获取混乱,此时可以用辅助的函数 👇
  155. ```ts
  156. import { createGetRoutes } from 'virtual:meta-layouts'
  157. const getRoutes = createGetRoutes(router)
  158. // 获取路由表但是不包含布局路由
  159. console.log(getRoutes())
  160. ```
  161. <br />
  162. <br />
  163. ## 实现 👀
  164. 布局实现思路来自 [vite-plugin-vue-layouts](https://github.com/JohnCampionJr/vite-plugin-vue-layouts)。
  165. 但用了更简单方案 👉 [虚拟文件](https://vitejs.cn/guide/api-plugin.html#importing-a-virtual-file) 与 [Glob 导入](https://vitejs.cn/guide/features.html#glob-import)。
  166. 该方案可以自动地做合理的 `hmr`。
  167. <br />
  168. <br />
  169. ## 组织 🦔
  170. 欢迎关注 **帝莎编程**
  171. - [官网](http://dishaxy.dishait.cn/)
  172. - [Gitee](https://gitee.com/dishait)
  173. - [Github](https://github.com/dishait)
  174. - [网易云课堂](https://study.163.com/provider/480000001892585/index.htm?share=2&shareId=480000001892585)
  175. <br />
  176. <br />
  177. ## License
  178. Made with [markthree](https://github.com/markthree)
  179. Published under [MIT License](./LICENSE).
  180. <br />