前言:
vue3+vite的项目中使用 element-plus的教程。
官方地址:
安装 | Element Plusa Vue 3 based component library for designers and developershttps://element-plus.gitee.io/zh-CN/guide/installation.html#%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9B%B4%E6%8E%A5%E5%BC%95%E5%85%A5
1、安装:
pnpm install element-plus -S
如果需要:cdn引入:index.html中加入下面的
<link rel="stylesheet" href="//unpkg.com/element-plus/dist/index.css" />
<script src="//unpkg.com/vue@3"></script>
<script src="//unpkg.com/element-plus"></script>
2、引入:
import 'element-plus/dist/index.css'
const app = createApp(App)
app.use(ElementPlus)
按需引入:需要安装额外的插件来支持
1)安装
npm install -D unplugin-vue-components unplugin-auto-import
2)vite.config.js
import { defineConfig } from 'vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
export default defineConfig({
// ...
plugins: [
// ...
AutoImport({
resolvers: [ElementPlusResolver()],
}),
Components({
resolvers: [ElementPlusResolver()],
}),
],
})
3、页面上使用:
组件官方地址:
Cascader 级联选择器 | Element Plusa Vue 3 based component library for designers and developershttps://element-plus.gitee.io/zh-CN/component/cascader.html#%E5%9F%BA%E7%A1%80%E7%94%A8%E6%B3%95
1、页面上直接用组件
<el-button>登录</el-button>
2、js中使用
import { ElMessage } from 'element-plus'
ElMessage('this is a message.')
ElMessage({
message: 'Congrats, this is a success message.',
type: 'success',
})