Web前端开发网

fly63.com

首页 资源 工具 文章 教程 栏目
  • 在线搜索
  • 文章标签
  • 广告合作
  • 赞助一下
  • 关于我们
资源推荐
阿里云优惠券
卓越的云计算技术和服务提供商
腾讯云优惠券
云服务器,云数据库,CDN,域名注册等多种云计算服务
扣子Coze
职场AI,就用扣子
豆包AI
字节跳动旗下 AI 智能助手
即梦AI
一站式智能创作平台,即刻造梦
AiPPT
全智能AI一键生成 PPT
堆友AI
零门槛,多风格AI绘画免费生成,电商海报设计神器
蜂小推
不扣量的项目推广平台
SpeedAI
一键去重、降AIGC率、数据可视化、论文写作

资源分类

AI智能 酷站推荐 招聘/兼职 框架/库 模块/管理 移动端UI框架 Web-UI框架 Js插件 Jquery插件 CSS相关 IDE环境 在线工具 图形动效 游戏框架 node相关 调试/测试 在线学习 社区/论坛 博客/团队 前端素材 图标/图库 建站资源 设计/灵感 IT资讯
网站收录 / 问题反馈

fast-vue3

分享
复制链接
新浪微博
QQ 好友

扫一扫分享

GitHub:https://github.com/tobe-fe-dalao/fast-vue3
网站描述:Vue3+Vite+Ts+Pinia+...一个快速开发vue3的模板框架
GitHub

一个开箱即用,快速搭建大型应用的vue3+Vite2+TypeScript+...模板框架。集成了各类插件,并进行了模块化和按需加载的优化,可以放心使用。 

大厂协作-代码规范

目前多数大厂团队一般使用husky和 lint-staged 来约束代码规范,

  • 通过pre-commit实现lint检查、单元测试、代码格式化等。
  • 结合VsCode编辑器(保存时自动执行格式化:editor.formatOnSave: true)
  • 配合Git hooks钩子(commit前或提交前执行:pre-commit => npm run lint:lint-staged)
  • IDE 配置(.editorconfig)、ESLint 配置(.eslintrc.js 和 .eslintignore)、StyleLint 配置(.stylelintrc 和 .stylelintignore),详细请看对应的配置文件。

关闭代码规范
将 src/ 目录分别加入 .eslintignore 和 .stylelintignore 进行忽略即可。

目录结构

以下是系统的目录结构

├── config
│   ├── vite             // vite配置
│   ├── constant         // 系统常量 
|   └── themeConfig      // 主题配置
├── docs                 // 文档相关
├── mock                 // mock数据    
├── plop-tpls            // plop模板   
├── src     
│    ├── api             // api请求   
│    ├── assets          // 静态文件   
│    ├── components      // 业务通用组件   
│    ├── page            // 业务页面 
│    ├── router          // 路由文件   
│    ├── store           // 状态管理   
│    ├── utils           // 工具类   
│    ├── App.vue         // vue模板入口   
│    ├── main.ts         // vue模板js
├── .d.ts                // 类型定义   
├── tailwind.config.js   // tailwind全局配置   
├── tsconfig.json        // ts配置
└── vite.config.ts       // vite全局配置  

支持JSX语法

{
    ...
    "@vitejs/plugin-vue-jsx": "^1.3.3"
    ...
}

UI组件按需加载,自动导入

//模块化写法
import Components from 'unplugin-vue-components/vite'
export const AutoRegistryComponents = () => {
    return Components({
        extensions: ['vue', 'md'],
        deep: true,
        dts: 'src/components.d.ts',
        directoryAsNamespace: false,
        globalNamespaces: [],
        directives: true,
        include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
        exclude: [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/],
        resolvers: [
            IconsResolver({
                componentPrefix: '',
            }),
            ArcoResolver({ importStyle: 'less' }),//根据你需要增加UI框架
            VueUseComponentsResolver(),//默认使用VueUse组件
        ],
    })
}

Vite插件模块化

为了方便管理插件,将所有的config统一放入config/vite/plugins里面,未来还会有更多插件直接分文件夹管理十分干净。 值得一提的是,Fast-Vue3增加了统一环境变量管理,来区分动态开启某些插件。

// vite/plugins/index.ts
/**
 * @name createVitePlugins
 * @description 封装plugins数组统一调用
 */
import type { Plugin } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import { ConfigSvgIconsPlugin } from './svgIcons';
import { AutoRegistryComponents } from './component';
import { AutoImportDeps } from './autoImport';
import { ConfigMockPlugin } from './mock';
import { ConfigVisualizerConfig } from './visualizer';
import { ConfigCompressPlugin } from './compress';
import { ConfigPagesPlugin } from './pages'
import { ConfigMarkDownPlugin } from './markdown'
import { ConfigRestartPlugin } from './restart'

export function createVitePlugins(isBuild: boolean) {
    const vitePlugins: (Plugin | Plugin[])[] = [
        // vue支持
        vue(),
        // JSX支持
        vueJsx(),
        // 自动按需引入组件
        AutoRegistryComponents(),
        // 自动按需引入依赖
        AutoImportDeps(),
        // 自动生成路由
        ConfigPagesPlugin(),
        // 开启.gz压缩  rollup-plugin-gzip
        ConfigCompressPlugin(),
        //支持markdown
        ConfigMarkDownPlugin(),
        // 监听配置文件改动重启
        ConfigRestartPlugin(),
    ];
    // vite-plugin-svg-icons
    vitePlugins.push(ConfigSvgIconsPlugin(isBuild));
    // vite-plugin-mock
    vitePlugins.push(ConfigMockPlugin(isBuild));
    // rollup-plugin-visualizer
    vitePlugins.push(ConfigVisualizerConfig());
    return vitePlugins;
}

而vite.config.ts便干净多了

import { createVitePlugins } from './config/vite/plugins'
...
return {
    resolve: {
      alias: {
        "@": path.resolve(__dirname, './src'),
        '@config': path.resolve(__dirname, './config'),
        "@components": path.resolve(__dirname, './src/components'),
        '@utils': path.resolve(__dirname, './src/utils'),
        '@api': path.resolve(__dirname, './src/api'),
      }
    },
    // plugins
    plugins: createVitePlugins(isBuild)
}
...

支持Pinia ,下一代Vuex5

创建文件src/store/index.ts

// 支持模块化,配合plop可以通过命令行一键生成
import { createPinia } from 'pinia';
import { useAppStore } from './modules/app';
import { useUserStore } from './modules/user';
const pinia = createPinia();
export { useAppStore, useUserStore };
export default pinia;

创建文件src/store/modules/user/index.ts

import { defineStore } from 'pinia'
import piniaStore from '@/store'
export const useUserStore = defineStore(
    // 唯一ID
    'user',
    {
        state: () => ({}),
        getters: {},
        actions: {}
    }
)

支持Plop自动生成文件

代码文件自动生成,提供三种预设模板pages,components,store,也可以根据自己需要设计更多自动生成脚本。一般后端同学惯用此形式,十分高效。

# 安装plop
pnpm add plop

根目录创建plopfile.ts

import { NodePlopAPI } from 'plop';
export default function (plop: NodePlopAPI) {
    plop.setWelcomeMessage('请选择需要创建的模式:')
    plop.setGenerator('page', require('./plop-tpls/page/prompt'))
    plop.setGenerator('component', require('./plop-tpls/component/prompt'))
    plop.setGenerator('store', require('./plop-tpls/store/prompt'))
}
# 启动命令
pnpm run plop

支持SVG图标

随着浏览器兼容性的提升,SVG的性能逐渐凸显,很多大厂团队都在创建自己的SVG管理库,后面工具库会有推荐。

# 安装svg依赖
pnpm add vite-plugin-svg-icons

配置vite.config.ts

import viteSvgIcons from 'vite-plugin-svg-icons';
export default defineConfig({
plugins:[
...
 viteSvgIcons({
    // 指定需要缓存的图标文件夹
    iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
    // 指定symbolId格式
    symbolId: 'icon-[dir]-[name]',
  }),
]
...
})

已封装一个简单的SvgIcon组件,可以直接读取文件下的svg,可以根据文件夹目录自动查找文件。

<template>
  <svg aria-hidden="true" :>
    <use :xlink:href="symbolId" :fill="color" />
  </svg>
</template>

<script lang="ts" setup>
const props = defineProps({
  prefix: {
    type: String,
    default: 'icon',
  },
  name: {
    type: String,
    required: true,
  },
  color: {
    type: String,
    default: '#333',
  },
  size: {
    type: String,
    default: 'default',
  },
})
const symbolId = computed(() => `#${props.prefix}-${props.name}`)
const calsses = computed(() => {
  return {
    [`sdms-size-${props.size}`]: props.size,
  }
})
const fontSize = reactive({ default: '32px', small: '20px', large: '48px' })
</script>

支持axios(ts版)

已封装了主流的拦截器,请求调用等方法,区分了模块index.ts/status.ts/type.ts

//封装src/api/user/index.ts
import request from '@utils/http/axios'
import { IResponse } from '@utils/http/axios/type'
import { ReqAuth, ReqParams, ResResult } from './type';
enum URL {
    login = '/v1/user/login',
    permission = '/v1/user/permission',
    userProfile = 'mock/api/userProfile'
}
const getUserProfile = async () => request<ReqAuth>({ url: URL.userProfile });
const login = async (data: ReqParams) => request({ url: URL.login, data });
const permission = async () => request<ReqAuth>({ url: URL.permission });
export default { getUserProfile, login, permission };
//调用
import userApi from "@api/user"
// setup模式下组件可以直接引用
const res = await userApi.profile()

自动生成router,过滤components组件

支持vue-router4.0的模块化,通过检索pages文件夹可自动生成路由,并支持动态路由

import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import routes from 'virtual:generated-pages'

console.log(routes,'打印生成自动生成的路由')
//导入生成的路由数据
const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

export default router

支持Mock数据

使用vite-plugin-mock插件,支持自动区分和启停的环境配置

// vite config
viteMockServe({
    ignore: /^\_/,
    mockPath: 'mock',
    localEnabled: !isBuild,
    prodEnabled: false,
    // https://github.com/anncwb/vite-plugin-mock/issues/9
    injectCode: `
       import { setupProdMockServer } from '../mock/_createProdMockServer';
       setupProdMockServer();
       `
    })

根目录下创建 _createProductionServer.ts文件,非_开头文件会被自动加载成mock文件

import { createProdMockServer } from 'vite-plugin-mock/es/createProdMockServer';
// 批量加载
const modules = import.meta.globEager('./mock/*.ts');

const mockModules: Array<string> = [];
Object.keys(modules).forEach((key) => {
    if (key.includes('/_')) {
        return;
    }
    mockModules.push(...modules[key].default);
});
export function setupProdMockServer() {
    createProdMockServer(mockModules);
}

Proxy代理

// vite config
import proxy from '@config/vite/proxy';
export default defineConfig({
    ...
    server: {
        hmr: { overlay: false }, // 禁用或配置 HMR 连接 设置 server.hmr.overlay 为 false 可以禁用服务器错误遮罩层
        // 服务配置
        port: VITE_PORT, // 类型: number 指定服务器端口;
        open: false, // 类型: boolean | string在服务器启动时自动在浏览器中打开应用程序;
        cors: false, // 类型: boolean | CorsOptions 为开发服务器配置 CORS。默认启用并允许任何源
        host: '0.0.0.0', // IP配置,支持从IP启动
        proxy,
    }
    ...
})
// proxy.ts
import {
    API_BASE_URL,
    API_TARGET_URL,
    MOCK_API_BASE_URL,
    MOCK_API_TARGET_URL,
} from '@config/constant';
import { ProxyOptions } from 'vite';
type ProxyTargetList = Record<string, ProxyOptions>;

const init: ProxyTargetList = {
    // test
    [API_BASE_URL]: {
        target: API_TARGET_URL,
        changeOrigin: true,
        rewrite: (path) => path.replace(new RegExp(`^${API_BASE_URL}`), ''),
    },
    // mock
    [MOCK_API_BASE_URL]: {
        target: MOCK_API_TARGET_URL,
        changeOrigin: true,
        rewrite: (path) => path.replace(new RegExp(`^${MOCK_API_BASE_URL}`), '/api'),
    },
};

export default init;

其他

  • 支持vw/vh移动端布局兼容,也可以使用plop自己配置生成文件
  • 还有更多新功能增在commiting,如果你有更好的方案欢迎PR

使用

一键三连: Star 或 Fork 或 可视化仓库

# 拉取仓库代码
git clone  https://github.com/tobe-fe-dalao/fast-vue3.git

# 进入项目文件夹
cd fast-vue3 

# 安装项目依赖
pnpm install

# 运行
pnpm run dev

如果不报错,恭喜你点火成功。否则,请看下面常见问题。

如果你已经了解本模板,建议你拉取 template 分支进行项目开发,该分支不含任何示例代码。

# clone  template 分支
git clone -b template https://github.com/tobe-fe-dalao/fast-vue3.git


仅供个人学习参考/导航指引使用,具体请以第三方网站说明为准,本站不提供任何专业建议。如果地址失效或描述有误,请联系站长反馈~感谢您的理解与支持!

链接: https://fly63.com/nav/3628

更多»
热门资源
bootstrap
最流行的HTML,CSS和JavaScript框架,用于开发响应式,移动端先行的web项目
官网
GitHub
Materialize
基于Material Design的现代响应式前端框架
官网
GitHub
Element UI
一套基于 Vue 2.0 的桌面端组件库
官网
GitHub
Material Design(MDB)
领先的Bootstrap UI套件之一
点击进入
Muse-UI
基于 Vue2.0 的 Material Design UI 库
官网
GitHub
sb-admin
基于Bootstrap简约美观的后台管理模板
官网
GitHub
Magic UI
为设计工程师打造的UI库
官网
GitHub
Vue-Access-Control
Vue权限管理解决方案
官网
GitHub
RmlUI
桌面端GUI开发框架
官网
GitHub
HeroUI
基于TailwindCSS构建的React 现代UI框架
官网
GitHub
tweakcn
shadcn/ui组件可视化无代码编辑器
官网
GitHub
kitty-ui
基于 Vue + Element 实现的权限管理系统
点击进入
GitHub
类似于fast-vue3的资源
kitty-ui
基于 Vue + Element 实现的权限管理系统
点击进入
GitHub
Grommet
一款基于React的UI框架
官网
GitHub
uiverse
纯css+html实现的精美UI网站
官网
TileBoard
一个简单的可定制的类似 Windows UI 框架
官网
GitHub
direflow
使用React创建快速、高性能的原生Web 组件
官网
GitHub
Appsmith
搭建企业级后台管理系统的开源利器
官网
GitHub
TOAST UI
用于构建高质量网页的开源js库,提供丰富的UI组件
官网
Bifrost UI
阿里跨平台多端适配 UI 组件库
官网
GitHub
目录

手机扫一扫预览

》
分享组件加载中...
首页 技术导航 在线工具 技术文章 教程资源 前端标签 AI工具集 前端库/框架 实用工具箱 广告合作 关于我们

Copyright © 2018 Web前端开发网提供免费在线工具、编程学习资源(教程/框架/库),内容以学习参考为主,助您解决各类实际问题,快速提升专业能力。