npm install element-plus
引入element-plus的方式是全局引入,代表的含义是所有的组件和插件都会被自动注册,
优点:上手快
缺点:会增大包的体积
在main.ts文件中
import { createApp } from 'vue' // 全局引入 import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' import App from './App.vue' import router from './router' import store from './store' const app = createApp(App) app.use(router) app.use(store) app.use(ElementPlus) app.mount('#app')
局部引入也就是在开发中用到某个组件对某个组件进行引入,
<template> <div class="app"> <el-button>Default</el-button> <el-button type="primary">Primary</el-button> <el-button type="success">Success</el-button> <el-button type="info">Info</el-button> <el-button type="warning">Warning</el-button> <el-button type="danger">Danger</el-button> <el-button>中文</el-button> </div> </template> <script lang="ts"> import { defineComponent } from 'vue' // 局部引入 import { ElButton } from 'element-plus' import 'element-plus/theme-chalk/el-button.css' import 'element-plus/theme-chalk/base.css' export default defineComponent({ components: { ElButton }, setup() { return {} } }) </script> <style lang="less"></style>
但是这样我们在开发时每次使用都要手动在组件中引入对应的css样式,使用起来会比较麻烦
需要安装unplugin-vue-components
和 unplugin-auto-import
这两款插件
npm install -D unplugin-vue-components unplugin-auto-import
安装完成之后在vue.config.js文件中配置
// vue.config.js const AutoImport = require('unplugin-auto-import/webpack') const Components = require('unplugin-vue-components/webpack') const { ElementPlusResolver } = require('unplugin-vue-components/resolvers') module.exports = { outputDir: './build', // 和webpapck属性完全一致,最后会进行合并 configureWebpack: { resolve: { alias: { components: '@/components' } }, //配置webpack自动按需引入element-plus, plugins: [ AutoImport({ resolvers: [ElementPlusResolver()] }), Components({ resolvers: [ElementPlusResolver()] }) ] } }
按需自动引入配置完之后,在组件中可直接使用,不需要引用和注册 这里已经实现了按需自动移入Element-plus组件 组件中直接使用:
<template> <div class="app"> <el-button>Default</el-button> <el-button type="primary">Primary</el-button> <el-button type="success">Success</el-button> <el-button type="info">Info</el-button> <el-button type="warning">Warning</el-button> <el-button type="danger">Danger</el-button> <el-button>中文</el-button> </div> </template> <script lang="ts"> import { defineComponent } from 'vue' export default defineComponent({ setup() { return {} } }) </script> <style lang="less"></style>
效果:
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理