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 效果

相关推荐
Eshine、1 天前
解决前端项目中,浏览器无法正常加载带.gz名称的文件
前端·vue3·.gz·.gz名称的js文件无法被加载
Light601 天前
Vue3 关键字速查表:从入门到进阶的全景指南
vue3·前端开发·响应式编程·组合式api·ai集成
你说啥名字好呢9 天前
【Vue 渲染流程揭秘】
前端·javascript·vue.js·vue3·源码分析
行走的陀螺仪12 天前
vue3-封装权限按钮组件和自定义指令
前端·vue3·js·自定义指令·权限按钮
麦麦大数据12 天前
F046 新闻推荐可视化大数据系统vue3+flask+neo4j
python·flask·vue3·知识图谱·neo4j·推荐算法
Sheldon一蓑烟雨任平生12 天前
Vue3 高级性能优化
性能优化·vue3·tree-shaking·高级性能优化·首屏加载优化·更新优化·大型虚拟列表
前端.攻城狮13 天前
用fetch-event-source处理流式消息:Vue 3中实现openAI/DeepSeek的实时输出
vue3·流式消息
Sheldon一蓑烟雨任平生14 天前
webpack 从零构建 Vue3
webpack·typescript·vue3·webpack配置·从零构建vue3
saadiya~15 天前
基于 Vue3 封装大华 RTSP 回放视频组件(PlayerControl.js 实现)
前端·vue3·大华视频相机前端播放
软件架构师-叶秋16 天前
Vue3+tyepescript+ElementPlus+Axios前端技术栈
前端·vue3·elementplus