f3-router
TypeScript icon, indicating that this package has built-in type declarations

0.0.25 • Public • Published

模板

https://gitee.com/codesmallwhite/f3-router-template.git

交流讨论

fanbook

使用步骤

  • 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

meta定义

在视图文件中 使用 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中的变量支持相对路径引入√
  • 美化输出文件

反馈微信:mm1669547593

Package Sidebar

Install

npm i f3-router

Weekly Downloads

65

Version

0.0.25

License

none

Unpacked Size

23.5 kB

Total Files

10

Last publish

Collaborators

  • small_code