Skip to content

Vue3-ElementPlus使用

1、安装引入

👉 安装ElementPlus

bash
yarn add element-plus --save

👉 main.ts中引入ElementPlus

bash
// 引入组件
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
const app = createApp(App)
app.use(createPinia()).use(ElementPlus)
app.mount('#app')

☞ 使用效果:

bash
<el-button type="primary">Primary</el-button>

image.png

你的main.ts完整应该如下

javascript
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
// 挂载router
import router from "./router/index" // 引入router
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
const app = createApp(App)
app.use(createPinia()).use(ElementPlus).use(router).mount('#app') //挂载

2、使用中文语言包

JS
import zhCn from 'element-plus/es/locale/lang/zh-cn' // 引入中文语言包

app.use(ElementPlus, { locale: zhCn }) // 全局设置语言为中文

3、表单使用

👉 表单重置

表单重置利用formRef获取表单实例,调用resetFields方法即可

JS
<template>
  <el-form ref="formRef" :model="numberValidateForm">
    <el-form-item label="age" prop="age" >
      <el-input v-model.number="numberValidateForm.age" type="text" autocomplete="off" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(formRef)">Submit</el-button>
      <el-button @click="resetForm(formRef)">Reset</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'

const formRef = ref<FormInstance>()
const numberValidateForm = reactive({
  age: '',
})

const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

Released under the MIT License.