网站首页 > 技术文章 正文
# 环境依赖
@vue/cli 4.0.5
vue 2.6.10"
element-ui 2.13.0
babel-plugin-component 1.1.1
全局安装
可以直接引入整个 Element ,使用 npm 进行安装:
npm i element-ui -S
然后在 main.js 中加入以下内容:
import Vue from 'vue'
import ElementUI from 'element-ui' // 引入框架
import 'element-ui/lib/theme-chalk/index.css' // 引入样式文件
import App from './App.vue'
Vue.use(ElementUI) // 注册
new Vue({
el: '#app',
render: h => h(App)
})
然后就可以直接在其他组件使用 Element 了。
<!-- Home.vue -->
<template>
<div class="home">
<el-button type="primary">按钮</el-button>
</div>
</template>
<script>
export default {
name: "home"
};
</script>
按需加载
当然也可以只引入需要的组件,以达到减小项目体积的目的。
首先安装一个按需加载的模块:
npm install babel-plugin-component -D
然后,将根目录下的 babel.config.js修改为:
module.exports = {
presets: ["@vue/cli-plugin-babel/preset"],
"plugins": [
[
"component",
{
"libraryName": "element-ui",
"styleLibraryName": "theme-chalk"
}
]
]
};
如果该文件不存在的话可以直接新建一个,或者新建一个 .babelrc 文件设置以下代码也是可以滴:
{
presets: ["@vue/cli-plugin-babel/preset"],
"plugins": [
[
"component",
{
"libraryName": "element-ui",
"styleLibraryName": "theme-chalk"
}
]
]
}
上面一步的配置修改好之后,在根目录下新建 ./plugins/element.js 文件:
import Vue from 'vue'
// 如果你只希望引入部分组件,比如 Button 和 Menu,那么就直接引入对应的组件名
import { Menu, Button } from 'element-ui'
Vue.use(Menu)
Vue.use(Button)
然后在 main.js 中引入 element.js 文件:
import Vue from 'vue'
import './plugins/element' // 注意只引入该文件
import App from './App.vue'
new Vue({
el: '#app',
render: h => h(App)
})
最后直接在需要用到 element 的组件中直接使用:
<!-- Home.vue -->
<template>
<div class="home">
<!-- 注意:element.js 中引入时是写的 Button,这里做组件使用时要在前面加上 el- -->
<el-button type="primary">按钮</el-button>
</div>
</template>
<script>
export default {
name: "home"
};
</script>
如何引入 notice 等组件
如果是全局引入 element-ui ,那么就可以直接在项目中按照以下方式使用 Notification、Message、MessageBox 等:
this.$notify(options);
this.$message(options);
this.$msgbox(options);
this.$alert(message, title, options) 或 this.$alert(message, options)
this.$confirm(message, title, options) 或 this.$confirm(message, options)
this.$prompt(message, title, options) 或 this.$prompt(message, options)
如果是按需加载引入 element-ui 则在需要用到的组件中进行引用:
<template>
<div class="login">
<button type="button" @click="submitLoginForm">提交</button>
</div>
</template>
<script>
// 第一步:引入 组件
import { Notification } from 'element-ui';
export default {
name: "Login",
data() {
return {
}
};
},
methods: {
submitLoginForm() {
// 第二步,调用
Notification({
title: '标题',
message: '这是提示文案'
});
}
}
};
</script>
也可以直接将 Notification 等方法添加到 Vue 的原型上:
// main.js
import { Notification, Message, MessageBox, Loading } from 'element-ui'
Vue.prototype.$notify = Notification
// Vue.prototype.$message = Message
// Vue.prototype.$msgbox = MessageBox
// Vue.prototype.$alert = MessageBox.alert
// Vue.prototype.$confirm = MessageBox.confirm
// Vue.prototype.$prompt = MessageBox.prompt
// Vue.prototype.$loading = Loading.service
然后直接在组件中使用:
this.$notify(options);
【喜欢我的文章欢迎 转发 点赞 与 关注,我会经常与大家分享前端的知识点的】
猜你喜欢
- 2024-09-10 一个基于vite构建的vue3+pinia+ts+element初始化开箱即用的项目
- 2024-09-10 GitHub一篇《Vue 加载远程组件解决方案》,引起业内大佬点评
- 2024-09-10 页面刷新时vuex数据持久化问题的解决方案:第三方插件完美实现
- 2024-09-10 基于vite + Vue3 + element-plus的前后端分离基础项目搭建
- 2024-09-10 Vue导入模块import xxx from '@/xxx'中的@是什么含义?
- 2024-09-10 终于搞懂了!原来 Vue 3 的 generate 是这样生成 render 函数的
- 2024-09-10 Vue3 + TS 中使用 Provide/Inject 需要考虑的三大问题
- 2024-09-10 关于Vue3的setup attribute标识是否是一个值得使用的语法糖?
- 2024-09-10 聊一聊如何在Vue中使用事件总线( Event Bus)进行组件间通信
- 2024-09-10 vite+vue3实现网页版编辑器,带高亮以及代码提
- 02-21走进git时代, 你该怎么玩?_gits
- 02-21GitHub是什么?它可不仅仅是云中的Git版本控制器
- 02-21Git常用操作总结_git基本用法
- 02-21为什么互联网巨头使用Git而放弃SVN?(含核心命令与原理)
- 02-21Git 高级用法,喜欢就拿去用_git基本用法
- 02-21Git常用命令和Git团队使用规范指南
- 02-21总结几个常用的Git命令的使用方法
- 02-21Git工作原理和常用指令_git原理详解
- 最近发表
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)