Element UI 是一套基于 Vue 2.0 的桌面端组件库,本文将结合示例代码介绍其基础使用方法。Element-UI 官方网站
在 HTML 的 <head> 中引入 Element UI 的样式表:
<link rel="stylesheet" href="https://unpkg.com/element-ui@2.15.14/lib/theme-chalk/index.css">
<script src="https://unpkg.com/element-ui@2.15.14/lib/index.js"></script>
在 Vue 实例化前,通过 Vue.use() 注册 Element UI:
Vue.use(ELEMENT);
按钮是最常用的交互组件,示例代码:
<el-button type="primary" round @click="addRandomObject" icon="el-icon-circle-plus" > 添加随机物体 </el-button>
主要属性说明:
用于操作后的反馈提示,示例代码:
this.$message({
message: `恭喜你,添加成功`,
type: 'success',
duration: 1500, // 显示时间(毫秒)
});
主要配置项:
完整的使用结构如下:
<div id="app">
<!-- Element UI 组件 -->
<el-button type="primary" @click="showMessage">点击我</el-button>
</div>
<script>
Vue.use(ELEMENT);
new Vue({
el: '#app',
methods: {
showMessage() {
this.$message.success('操作成功!');
}
}
});
</script>
Element UI 提供了丰富的 UI 组件和便捷的 API,是 Vue2 项目中快速构建企业级应用的理想选择。掌握其基础用法后,可以大大提高开发效率。