Getting Started with Element UI: Installation and Usage
🎯 Introduction
In this article, you will learn why frontend projects use UI frameworks, what Element UI is, and how to install and use it in Vue 2. These skills help you build attractive, efficient interfaces faster without repeatedly implementing common components.
⚙️ Why Use Element UI?
🤔 What Makes Building Components from Scratch Difficult?
Frontend applications regularly need buttons, forms, dialogs, and navigation. Building these with plain HTML and CSS offers flexibility, but comes with several challenges:
- Styling takes time and can introduce compatibility issues
- Interactions such as dialog animations and form validation require substantial JavaScript
- Components can be difficult to reuse across projects
- Inconsistent UI designs increase maintenance costs
For example, building a form with validation can take hundreds of lines of validation logic alone. You also need clear, well-styled error messages to provide a good user experience.
🚀 How Does a UI Framework Improve Productivity?
Many teams use UI frameworks to address these challenges. A framework packages common components for you, offering several advantages:
- Build interfaces quickly with minimal configuration
- Keep styles consistent throughout the application
- Use built-in interactions instead of implementing them repeatedly
- Find examples and documentation through an active community
- Reduce bundle size with on-demand imports
A UI framework lets you implement more functionality with less code, speeding up development and making maintenance easier.
🎨 What Is Element UI?
Element UI is a Vue 2 component library for designers and developers. Created by the Ele.me frontend team, it is open source, well documented, and offers a clean design language and enough components for most administration interfaces.
Its features include:
- Built for Vue 2, with APIs that follow Vue’s design principles
- A wide range of buttons, forms, dialogs, menus, tables, and more
- Theme customization to match your brand
- Detailed documentation and examples to help you get started
- An active community that responds to issues and feature requests
Element UI is one of the best-known UI libraries in the Vue 2 ecosystem and is particularly suited to administration interfaces.
🛠️ Installing Element UI in a Vue 2 Project
You can use Element UI through full imports or on-demand imports. First, install the dependency.
1. Install the Dependency
Make sure your project uses Vue 2 and that npm or yarn is available, then run the following from the project root:
npm install element-ui --save
# Or use yarn
yarn add element-ui
This downloads Element UI’s code and styles into your project’s node_modules directory.
2. Import and Register Element UI
Element UI supports both full imports and on-demand imports.
✨ Importing All of Element UI
A full import loads all Element UI components and styles at once. It is easy to configure and useful for small projects or initial experiments.
Add the following to main.js:
import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
new Vue({
el: '#app',
render: h => h(App),
});
All Element UI components are now available throughout the project without individual imports.
🎉 Usage Example:
<template>
<el-button type="primary">This is a button</el-button>
</template>
🔍 On-Demand Imports
On-demand imports include only the components you use, keeping the bundle smaller.
1. Install babel-plugin-component
npm install babel-plugin-component -D
# Or
yarn add babel-plugin-component -D
2. Update babel.config.js or .babelrc:
module.exports = {
plugins: [
[
'component',
{
libraryName: 'element-ui',
styleLibraryName: 'theme-chalk',
},
],
],
};
3. Import Components as Needed:
import Vue from 'vue';
import { Button, Select } from 'element-ui';
Vue.component(Button.name, Button);
Vue.component(Select.name, Select);
new Vue({
el: '#app',
render: h => h(App),
});
This imports only Button and Select; other components are excluded from the bundle.
🎉 Usage Example:
<template>
<div>
<el-button type="success">Success button</el-button>
<el-select v-model="value" placeholder="Select an option">
<el-option label="Option 1" value="1" />
<el-option label="Option 2" value="2" />
</el-select>
</div>
</template>
<script>
export default {
data() {
return {
value: '',
};
},
};
</script>
❓ Questions and Answers
Q1: Does Element UI support Vue 3?
Element UI targets Vue 2. Use Element Plus for Vue 3.
Q2: Why are styles missing when using on-demand imports?
The babel-plugin-component plugin may be missing or misconfigured, or the style library path may be incorrect.
🧪 Exercises
Exercise 1
Render your first button using a full import.
<template>
<el-button type="primary">Button component</el-button>
</template>
Exercise 2
Render a dropdown using on-demand imports.
<template>
<el-select v-model="gender" placeholder="Select a gender">
<el-option label="Male" value="male" />
<el-option label="Female" value="female" />
</el-select>
</template>
<script>
export default {
data() {
return {
gender: '',
};
},
};
</script>
🎉 You now understand why Element UI is useful, what it provides, and how to install and use it in Vue 2 with full and on-demand imports!
