vue3整合element-plus

为项目命名 选择vue 框架 选择TS

启动测试: npm run dev

开始整合 element-plus

javascript 复制代码
npm install element-plus --save
npm install unplugin-vue-components unplugin @vitejs/plugin-vue --save-dev

修改main.ts

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

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

修改vite.config.ts

javascript 复制代码
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default defineConfig({
  plugins: [
    vue(),
    Components({
      resolvers: [ElementPlusResolver()],
      dts: true, // 自动生成一个 components.d.ts 文件,帮助 TS 识别
    }),
  ],
})

检查 tsconfig.app.json

javascript 复制代码
{
  "files": [],
  "references": [
    { "path": "./tsconfig.app.json" },
    { "path": "./tsconfig.node.json" }
  ]
}

修改 tsconfig.app.json

javascript 复制代码
{
  "extends": "@vue/tsconfig/tsconfig.dom.json",
  "compilerOptions": {
    "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "erasableSyntaxOnly": true,
    "noFallthroughCasesInSwitch": true,
    "noUncheckedSideEffectImports": true,
    "types": [
      "element-plus/global"
    ]
  },
  "include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"]
}

修改 App.vue

javascript 复制代码
<template>
  <Login/>
</template>

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

<style scoped>
</style>

Login.vue

javascript 复制代码
<template>
  <div class="login-container">
    <el-card class="box-card">
      <template #header>
        <div>用户登录</div>
      </template>
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" show-password />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">登录</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";

const form = ref({ username: "", password: "" });
const rules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
};
const formRef = ref();

function onSubmit() {
  if (formRef.value) {
    formRef.value.validate((valid) => {
      if (valid) alert("提交成功");
    });
  }
}

function onReset() {
  if (formRef.value) {
    formRef.value.resetFields();
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.box-card {
  width: 400px;
}
</style>

npm run dev 效果

相关推荐
Cherry的跨界思维15 小时前
28、AI测试环境搭建与全栈工具实战:从本地到云平台的完整指南
java·人工智能·vue3·ai测试·ai全栈·测试全栈·ai测试全栈
Cherry的跨界思维2 天前
【AI测试全栈:Vue核心】22、从零到一:Vue3+ECharts构建企业级AI测试可视化仪表盘项目实战
vue.js·人工智能·echarts·vue3·ai全栈·测试全栈·ai测试全栈
前端小L2 天前
专题三:完善响应式 —— readonly 与 isReactive
源码·vue3
神色自若2 天前
vue3 带tabs的后台管理系统,切换tab标签后,共用界面带参数缓存界面状态
前端·vue3
前端小L3 天前
专题一:搭建测试驱动环境 (TypeScript + Vitest)
前端·javascript·typescript·源码·vue3
前端小L3 天前
专题二:核心机制 —— reactive 与 effect
javascript·源码·vue3
Cherry的跨界思维5 天前
【AI测试全栈:Vue核心】19、Vue3+ECharts实战:构建AI测试可视化仪表盘全攻略
前端·人工智能·python·echarts·vue3·ai全栈·ai测试全栈
Misha韩7 天前
vue3 实时通讯 SSE
vue3·sse·实时通讯
冥界摄政王10 天前
Cesium学习第二章 camera 相机
node.js·html·vue3·js·cesium
Irene199111 天前
在 Vue3 中使用 Element Plus
vue3·element plus