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

相关推荐
之歆6 天前
Vue3 + Vite2.0 全栈开发实践:从零到一构建通用后台管理系统-上
vue3·vite2.0
之歆6 天前
Vue3 + Vite2.0 全栈开发实践:从零到一构建通用后台管理系统-下
javascript·vue.js·vue3
麦麦大数据8 天前
M004_基于Langchain+RAG的银行智能客服系统设计与开发
typescript·langchain·flask·vue3·faiss·rag
哆啦A梦158810 天前
Vue3魔法手册 作者 张天禹 012_路由_(一)
前端·typescript·vue3
麦麦大数据12 天前
M003_中药可视化系统开发实践:知识图谱与AI智能问答的完美结合
人工智能·flask·llm·vue3·知识图谱·neo4j·ner
哆啦A梦158812 天前
Vue3魔法手册 作者 张天禹 015_插槽
前端·vue.js·typescript·vue3
沛沛老爹17 天前
Vue3+TS实战:基于策略模式的前端动态脱敏UI组件设计与实现
前端·ui·vue3·数据安全·策略模式·动态渲染·前端脱敏
gsls20080818 天前
vue3学习笔记
笔记·vue3
平头也疯狂20 天前
RuoYi Office 全景介绍:一个平台管好整个企业
微服务·vue3·springboot·crm·oa·企业管理系统
weixin79893765432...21 天前
vue3 系统的梳理
vue.js·vue3