vue管理后台搭建

1.vite模板创建

打开vite官网选择你要创建的模板,这里我选择vue-ts模板

复制代码
yarn create vite vue-admin --template vue-ts

到目录,然后运行yarn安装项目依赖,运行yarn dev启动项目,运行端口http://localhost:5173/http://localhost:5173/

2.配置项目

1.配置项目别名@

1.引入@types/node

@types/node内部包含了对Node.js 核心模块及常用第三方库的类型信息描述,增加了TypeScript 对 Node.js 环境的支持

复制代码
yarn add @types/node -D
2.在tsconfig.json文件中添加配置项

在tsconfig.json文件中的compilerOptions对象内添加配置项

复制代码
"compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@/*":["src/*"]
    }
}

3.在vite.config.ts文件中添加配置项

复制代码
    import { defineConfig } from "vite";
    import vue from "@vitejs/plugin-vue";
    import { resolve } from "path";

    // https://vite.dev/config/
    export default defineConfig({
      plugins: [vue()],
      resolve: {
        alias: {
          "@": resolve(__dirname, "./src"),
        }
      },
    });
4.项目可能会有ts提示找不到xxx文件的情况

找到vite-env.d.ts文件,没有的可以创建一个在根目录,代码如下:

复制代码
    /// <reference types="vite/client" />
    declare module "*.vue" {
      import { ComponentOptions } from "vue";
      const componentOptions: ComponentOptions;
      export default componentOptions;
    }
    declare module "vue-router"

3.安装依赖

1.安装sass预处理器

复制代码
yarn add sass sass-loader --dev

2.安装element-plus

复制代码
yarn add element-plus

如果您的终端提示 legacy JS API Deprecation Warning, 您可以配置以下代码在 vite.config.ts 中.

复制代码
css: {
  preprocessorOptions: {
    scss: { api: 'modern-compiler' },
  }
}

main.ts中完整引入element-plus

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

const app = createApp(App)

app.use(ElementPlus)
app.mount('#app')

3.安装vue-router

复制代码
yarn add vue-router@4
1.新建router/index.ts文件
复制代码
import { createRouter, createWebHistory } from 'vue-router'
 
const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('@/views/home/index.vue')
  }
]
 
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})
 
export default router
2.App.vue 文件修改

RouterView是路由的出口

复制代码
<template>
  <div>
    <RouterView />
  </div>
</template>

<script setup lang="ts">
</script>

<style lang="scss" scoped>
</style>
3.main.ts 文件修改

引入router文件

复制代码
import { createApp } from 'vue'
import App from './App.vue'
import router from "@/router/index"
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

const app = createApp(App)
app.use(ElementPlus)
app.use(router)
app.mount('#app')

未完待续。。。

相关推荐
百***35512 分钟前
TypeScript 与后端开发Node.js
javascript·typescript·node.js
启山智软8 分钟前
使用 Spring Boot + Vue.js 组合开发多商户商城(B2B2C平台)是一种高效的全栈技术方案
vue.js·spring boot·后端
hongliangsam11 分钟前
Vue 3 defineModel 完全指南
vue.js
ZT_KeBei15 分钟前
前端调试利器——pageSpy的使用简易指南
前端
少卿18 分钟前
PerformanceObserver 性能条目类型(Entry Types)
前端·javascript
宇余19 分钟前
ES2025新特性实战:5分钟get前端高频实用语法
前端·typescript
励扬程序19 分钟前
Cocos Creator 3.8 实现指定Node节点截图功能教程
前端·cocos creator
jenchoi41324 分钟前
【2025-11-15】软件供应链安全日报:最新漏洞预警与投毒预警情报汇总
前端·网络·安全·网络安全·npm·node.js
进击的野人24 分钟前
防抖与节流:优化前端性能的两种关键技术
前端·javascript
小高00726 分钟前
别再滥用 Base64 了——Blob 才是前端减负的正确姿势
前端·javascript·面试