使用Vite创建VUE3+TS+Eslint+Prettier+Pinia+VueRouter+Axios项目

1. 创建一个vite项目

pnpm create vite
√ Project name: ... (项目名)
√ Select a framework: » Vue
√ Select a variant: » TypeScript

Scaffolding project in D:\vite-project...

Done. Now run:

  cd (项目名)
  pnpm install   
  pnpm run dev

2. Eslint+Prettier安装及配置

2.1 Eslint安装及初始化
pnpm add -D eslint
npx eslint --init

// 初始化选项
√ How would you like to use ESLint? · problems    
√ What type of modules does your project use? · esm
√ Which framework does your project use? · vue
√ Does your project use TypeScript? · No / Yes
√ Where does your code run? · browser, node
√ What format do you want your config file to be in? · JavaScript
The config that you've selected requires the following dependencies:
eslint-plugin-vue@latest @typescript-eslint/eslint-plugin@latest @typescript-eslint/parser@latest
√ Would you like to install them now? · No / Yes
√ Which package manager do you want to use? · pnpm
Installing eslint-plugin-vue@latest, @typescript-eslint/eslint-plugin@latest, @typescript-eslint/parser@latest
// package.json 添加脚本
"scripts": {
	"lint": "eslint . --ext .vue,.js,.ts,.jsx,.tsx --fix",
},

// 测试运行
pnpm lint
// 有报错,先不管
> vite-project@0.0.0 lint D:\vite-project
> eslint . --ext .vue,.js,.ts,.jsx,.tsx --fix
D:\vite-project\src\App.vue
  1:8  error  Parsing error: '>' expected
D:\vite-project\src\components\HelloWorld.vue
  1:8  error  Parsing error: '>' expected
✖ 2 problems (2 errors, 0 warnings)ELIFECYCLE  Command failed with exit code 1.
2.2 Prettier安装及配置
pnpm add -D prettier eslint-plugin-prettier eslint-config-prettier
// .editorconfig 根目录下创建
# https://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
indent_style = tab
insert_final_newline = false
trim_trailing_whitespace = false
// .prettierrc.cjs 根目录下创建
module.exports = {
    printWidth: 80, // 单行长度
    tabWidth: 4, // 缩进长度
    useTabs: false, // 使用空格代替tab缩进
    semi: false, // 句末使用分号
    singleQuote: true, // 使用单引号
}
// .eslintrc.cjs 修改
module.exports = {
    env: {
        browser: true,
        es2021: true,
        node: true,
        'vue/setup-compiler-macros': true,
    },
    extends: [
        'eslint:recommended',
        'plugin:vue/vue3-recommended',
        'plugin:@typescript-eslint/recommended',
        'plugin:prettier/recommended',
    ],
    overrides: [],
    parser: 'vue-eslint-parser',
    parserOptions: {
        ecmaVersion: 'latest',
        parser: '@typescript-eslint/parser',
        sourceType: 'module',
    },
    plugins: ['vue', '@typescript-eslint'],
    rules: {},
}

// package.json 添加脚本
"scripts": {
	"format": "prettier --write ./**/*.{vue,ts,tsx,js,jsx,css,less,scss,json,md}"
},

// 测试运行
pnpm format

// 运行结果
> vite-project@0.0.0 format D:\vite-project
> prettier --write ./**/*.{vue,ts,tsx,js,jsx,css,less,scss,json,md}
.vscode\extensions.json 47ms
package.json 6ms
README.md 53ms
src\App.vue 249ms
src\components\HelloWorld.vue 49ms
src\main.ts 7ms
src\style.css 19ms
src\vite-env.d.ts 9ms
tsconfig.json 6ms
tsconfig.node.json 3ms
vite.config.ts 6ms

3. 基础依赖(Pinia+VueRouter+Axios)

3.1 Pinia安装
pnpm add pinia pinia-plugin-persistedstate
// main.ts 修改
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'

import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'

createApp(App).use(createPinia().use(piniaPluginPersistedstate)).mount('#app')
3.1 VueRouter安装及配置
pnpm add vue-router
// App.vue 修改
<script setup lang="ts"></script>

<template>
    <router-view />
</template>

<style scoped lang="scss"></style>
// src目录下新建views文件夹
// 新建 index.vue 文件随便添加点内容

<script setup lang="ts">
import HelloWorld from '../components/HelloWorld.vue'
</script>

<template>
    <div>
        <a href="https://vitejs.dev" target="_blank">
            <img src="/vite.svg" class="logo" alt="Vite logo" />
        </a>
        <a href="https://vuejs.org/" target="_blank">
            <img src="./assets/vue.svg" class="logo vue" alt="Vue logo" />
        </a>
    </div>
    <HelloWorld msg="Vite + Vue" />
</template>

<style scoped>
.logo {
    height: 6em;
    padding: 1.5em;
    will-change: filter;
    transition: filter 300ms;
}
.logo:hover {
    filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vue:hover {
    filter: drop-shadow(0 0 2em #42b883aa);
}
</style>
// main.ts 修改
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'

import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'

import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

// 路由
const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'index',
        component: () => import('./views/index.vue'),
        meta: {
            title: '首页',
        },
    },
]

createApp(App)
    .use(createPinia().use(piniaPluginPersistedstate))
    .use(
        createRouter({
            history: createWebHashHistory(),
            routes: routes,
        })
    )
    .mount('#app')
3.2 Axios安装及配置
pnpm add axios
// src 下新建目录utils 
// 新建axios.ts文件
import axios, { AxiosRequestConfig } from 'axios'

/*
 * 创建Axios请求
 */
export const createAxios = (axiosConfig: AxiosRequestConfig) => {
    const Axios = axios.create({
        baseURL: '',
        timeout: 1000 * 10,
        headers: {
            'Content-Type': 'application/json',
        },
        responseType: 'json',
        method: 'POST',
    })
    // 请求拦截
    Axios.interceptors.request.use(
        (config) => {
            // 请求前处理
            return config
        },
        (error) => {
            return Promise.reject(error)
        }
    )

    // 响应拦截
    Axios.interceptors.response.use(
        (response) => {
            // 响应成功处理
            return response.data
        },
        (error) => {
            return Promise.reject(error)
        }
    )
    return Axios(axiosConfig)
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于vite+vue3+pinia+element-plus+ts后台管理系统源码+项目说明.zip ## 简介 vue-element-perfect 是一个后台前端解决方案,它使用了最新的前端技术栈、动态路由,权限验证,并且有着丰富的组件,企业级中后台解决方案,可免费商用,同时支持PC、平板、手机 ## 项目功能 - 使用Vue3.0开发,单文件组件采用<script setup> - 采用 Vite3 作为项目开发、打包工具(配置了 Gzip 打包、TSX 语法、跨域代理) - 整个项目集成了 TypeScript - 登录逻辑,使用vue-router进行路由权限拦截,判断,路由懒加载 - 使用 keep-alive 对整个页面进行缓存,支持多级嵌套页面 - 侧边栏导航菜单栏动态的显示 - 各种可视化地图组件 - 使用 Pinia替代 Vuex,轻量、简单、易用 - 导出excel,自定义样式导出excel、多表头导出 - 表单、表格、水印、多标签显示、打印功能,图片打印、表格打印、普通打印、二维码、拖拽、markdown、头像裁剪、图片上传... - 使用 Prettier 统一格式化代码,集成 Eslint、代码校验规范 ## 安装 ``` ## 分支管理 - master 技术采用 vite + vue3.0 + Typescript + pinia - vue-admin-simple 简易版本 - vite-vuex vite + vue3.0 + Typescript + vuex - vue-i18n 语言切换版本 - webpack 技术采用 webpack + vue3.0 + Typescript + vuex - uniapp uniapp版本 uniapp +vuex +element scss ``` # 本地开发 启动项目 借助hbuilder工具运行浏览器启动 ``` ## 下载依赖 ``` npm install cnpm install yarn # npm install 安装失败,请升级 nodejs 到 16 以上,或尝试使用以下命令: npm install --registry=https://registry.npm.taobao.org ``` ## 运行打包 ``` npm run dev npm run build ``` ## eslint+prettier ``` # eslint 检测代码 npm run lint #prettier 格式化代码 npm run lint:prettier ``` ## 文件目录结构 ``` vue-admin-perfect ├─ public # 静态资源文件(忽略打包) ├─ src │ ├─ api # API 接口管理 │ ├─ assets # 静态资源文件 │ ├─ components # 全局组件 │ ├─ config # 全局配置项 │ ├─ hooks # 常用 Hooks │ ├─ language # 语言国际化 │ ├─ layout # 框架布局 │ ├─ routers # 路由管理 │ ├─ store # pinia store │ ├─ styles # 全局样式 │ ├─ utils # 工具库 │ ├─ views # 项目所有页面 │ ├─ App.vue # 入口页面 │ └─ main.ts # 入口文件 ├─ .env # vite 常用配置 ├─ .env.development # 开发环境配置 ├─ .env.production # 生产环境配置 ├─ .env.test # 测试环境配置 ......
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值