Vue3+elementPlus+Vite项目

Vue3+elementPlus+Vite项目

  • [1. 项目创建](#1. 项目创建)
    • [1.1. 使用pnpm创建项目](#1.1. 使用pnpm创建项目)
    • [1.2. 启动项目](#1.2. 启动项目)
    • [1.3. 配置项目`url`映射](#1.3. 配置项目url映射)
    • [1.4. 安装依赖包](#1.4. 安装依赖包)
  • [2. vue3路由](#2. vue3路由)
    • [2.1. 添加路由配置文件](#2.1. 添加路由配置文件)
    • [2.2. 注册路由](#2.2. 注册路由)
  • [3. 注册elemet-plus](#3. 注册elemet-plus)
  • [4. Pinia](#4. Pinia)

1. 项目创建

1.1. 使用pnpm创建项目

shell 复制代码
> pnpm create vite --template=vue-ts
◇  Project name:
│  gcs # 输入项目名称

# 是否使用 npm 进行安装并立即启动
◆  Install with npm and start now?
│  ○ Yes / ● No
└

1.2. 启动项目

shell 复制代码
> cd gcs 
> pnpm install
> pnpm dev

1.3. 配置项目url映射

json 复制代码
// 修改tsconfig.app.json
{
    // ...
    "compilerOptions": {
				// ...
        "baseUrl": ".",          // 添加跟路径 .
        "paths": {							// url映射
            "@/*": ["src/*"]
        }
    },
	// ...
}

// 修改vite.config.ts
export default defineConfig({
    // ...
    resolve: {
        alias: {
            "@": path.resolve(__dirname, "src"),
        },
    },
    // ...
});

1.4. 安装依赖包

shell 复制代码
> pnpm install element-plus
> pnpm install @element-plus/icons-vue
> pnpm install vue-router

2. vue3路由

2.1. 添加路由配置文件

添加router目录,并在该目录项添加index.ts

路径:../src/route/index.ts

ts 复制代码
/*
../srt/route/index.ts
*/  
import { createRouter, createWebHistory } from "vue-router";

const routes = [
    {
        path: "/",
        name: "user_info",
        component: import("../sys/UserInfo.vue"),
    },
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

export default router;

2.2. 注册路由

ts 复制代码
/*
../srt/main.ts
*/
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

const app = createApp(App)
app.use(router) // 注册路由
app.mount('#app')

3. 注册elemet-plus

ts 复制代码
/*
../srt/main.ts
*/
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

const app = createApp(App)
app.use(router) // 注册路由
app.mount('#app')
app.use(ElementPlus)  // 注册elemet-plus

4. Pinia

shell 复制代码
pnpm i pinia

注册Pinia

ts 复制代码
/*
../srt/main.ts
*/
import { createApp } from "vue";
import "./style.css";
import App from "./App.vue";
import { createPinia } from 'pinia'

const app = createApp(App);
const pinia = createPinia()
app.use(pinia);
app.mount("#app");
相关推荐
汤米粥1 天前
2026年主流前端技术框架
vue·react·elementplus·tailwind css·angular‌·svelte‌·bootstrap‌
codigger1 天前
Vue nextTick 到底在等什么?一篇讲透原理与那些年踩过的坑
程序员·vue·编程·异步更新·#人工智能
咚咚咚小柒2 天前
【前端】LangChain.js学习
前端·javascript·人工智能·typescript·langchain·vue·ai编程
悟道书生4 天前
若依 RBAC 权限控制学习笔记
java·网络·笔记·sql·学习·vue
mewowaBc4 天前
使用Pinia持久化存储实现用户认证系统
vue·pinia·用户认证系统
gc_22995 天前
使用VS Code终端创建Vue项目报错:“npm : 无法加载文件 ...\npm.ps1,因为在此系统上禁止运行脚本”
vue·vs code
小阿鑫5 天前
尤雨溪 : Vue 4 已经在规划中了!
vue·vite·vite+·程序员海军·vue4
离歌漠6 天前
《我用 Tauri 做了一个本地开发工具箱》
rust·typescript·vue
enjoy编程6 天前
playwright学习-I
typescript·vue·nodejs·1024程序员节·playwright
落花看风雪9 天前
从零开始搭建后台系统(vue3+TypeScript+Vite)01
typescript·vue·js