在使用vue cli3脚手架时,需要按需导入element-ui 组件,步骤如下:
1. 安装element-ui
1 | npm i element-ui -S |
2. 按需导入需要安装,babel-plugin-component
1 | npm install babel-plugin-component -D |
3.官网提供的是,将 .babelrc 修改为:
1 2 3 4 5 6 7 8 9 10 11 12 | { "presets" : [[ "es2015" , { "modules" : false }]], "plugins" : [ [ "component" , { "libraryName" : "element-ui" , "styleLibraryName" : "theme-chalk" } ] ] } |
注意:项目中没有.babelrc文件,无需新建.babelrc文件,直接可以在babel.config.js中配置即可,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | module.exports = { presets: [ '@vue/app' , [ "@babel/preset-env" , { "modules" : false }] ], plugins: [ [ "component" , { "libraryName" : "element-ui" , "styleLibraryName" : "theme-chalk" } ] ] } |
这里需要注意:
需要安装最新的babel编译插件“@babel/preset-env”,检查一下自己项目插件版本,如果不是最新版,可执行以下命令安装
1 | npm install @babel/preset-env -D |
4.在main.js中导入你需要的部分组件,如Button
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import Vue from 'vue' ; import { Button } from 'element-ui' ; import App from './App.vue' ; Vue.component(Button.name, Button); Vue.component(Select.name, Select); /* 或写为 * Vue.use(Button) * Vue.use(Select) */ new Vue({ el: '#app' , render: h => h(App) }); |
5. 在App.vue中,加入以下代码
1 2 3 4 5 6 7 8 9 10 | <template> <div id= "app" > <el-button>默认按钮</el-button> <el-button type= "primary" >主要按钮</el-button> <el-button type= "success" >成功按钮</el-button> <el-button type= "info" >信息按钮</el-button> <el-button type= "warning" >警告按钮</el-button> <el-button type= "danger" >危险按钮</el-button> </div> </template> |
如果在浏览器中运行,出现如下样式,说明按需导入配置成功:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!