https://gitee.com/codesmallwhite/f3-router-template.git
- npm add vue-router
- npm add f3-router -D
- vite配置中使用
//+++++++++
import f3Router from 'f3-router'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue(),
//+++++++++
f3Router()],
})
- 在main.js中使用
//frouter目录是插件生成的
//+++++++++
import filerouter from './frouter/router'
createApp(App)
//+++++++++
.use(filerouter())
.mount('#app')
- 在app.vue中加入
<script setup>
//+++++++++
import { RouterView } from 'vue-router';
</script>
<template>
<!-- ++++++++++ -->
<RouterView></RouterView>
</template>
- 在views文件夹新增页面
- npm run dev
在视图文件中 使用 defineMeta定义meta
defineMeta({
lazy: 'false' //默认true,
withLayout:'false' //默认ture(如果存在Layout文件)
beforeEnter: (to, from) => {
console.log(to, from)
}
})
注意meta中使用的变量只能从外部导入,不能直接使用当前文件定义的变量 导入变量必须是绝对路径/别名路径导入,相对路径可能无法识别 // 动态路由[可选参数]
需要注意的是,满足以下条件才会被注册为路由,
不以 . 或 _ 开头的文件或目录
非components 和 component 目录
非utils 和 util 目录
是 .jsx、 .tsx 文件
动态可选路由 约定 [ $] 包裹的文件或文件夹为动态可选路由。
比如:
src/pages/users/[id$].tsx 会成为 /users/:id?
src/pages/users/[id$]/settings.tsx 会成为 /users/:id?/settings
动态路由 约定 [] 包裹的文件或文件夹为动态路由。
比如:
src/pages/users/[id].tsx 会成为 /users/:id
src/pages/users/[id]/settings.tsx 会成为 /users/:id/settings
约定目录下有 layout.ext 时会生成嵌套路由,以 该文件 为该目录的 layout。 比如以下目录结构,
.
└── pages
└── users
├── layout.tsx
├── index.tsx
└── list.tsx
如果某文件不需要layout,则在该文件插入下面的代码片段
defineMeta({
withLayout: false
})
TIPS:不要同时存在 file file/index 这种结构
如:
└── users.tsx
└── users
└── index.tsx
TODOLIST
- 自定义页面文件夹
- 自定义路由文件生成目录
- Meta中的变量支持相对路径引入√
- 美化输出文件