A Quick Guide to Common Components
🎯 Introduction
Element UI is a popular Vue 2 component library that simplifies development with a wide range of UI components. This article introduces buttons, inputs, selects, date pickers, forms, and message components so you can build functional, user-friendly applications.
🛠️ Button
Buttons are among the most common interactive elements on the web. Element UI provides several types and states for different situations.
Basic Usage
The default button type is default. Other common types include primary, success, warning, danger, info, and text. Use the type attribute to choose one.
<template>
<el-button>Default button</el-button>
<el-button type="primary">Primary button</el-button>
<el-button type="success">Success button</el-button>
<el-button type="warning">Warning button</el-button>
<el-button type="danger">Danger button</el-button>
<el-button type="text">Text button</el-button>
</template>
Loading, Disabled, and Icon Buttons
Buttons can show a loading state to prevent repeated clicks, be disabled, or include an icon.
<template>
<el-button type="primary" :loading="loading" @click="handleClick">Loading</el-button>
<el-button type="primary" disabled>Disabled button</el-button>
<el-button type="primary" icon="el-icon-search">Search</el-button>
<el-button icon="el-icon-edit"></el-button>
</template>
<script>
export default {
data() {
return {
loading: false,
};
},
methods: {
handleClick() {
this.loading = true;
setTimeout(() => {
this.loading = false;
}, 2000);
},
},
};
</script>
✍️ Input
Inputs let users enter text. Element UI’s Input supports several additional features.
Two-Way Binding with v-model
Use Vue’s v-model for two-way binding to keep your data updated as the user types.
<template>
<el-input v-model="inputValue" placeholder="Enter text"></el-input>
<p>You entered: {{ inputValue }}</p>
</template>
<script>
export default {
data() {
return {
inputValue: '',
};
},
};
</script>
Prefix and Suffix Icons, Clear Buttons, and Disabled State
Inputs can include icons on either side, display a clear button, or be disabled.
<template>
<el-input placeholder="Prefix icon" prefix-icon="el-icon-search" v-model="searchText" />
<el-input placeholder="Suffix icon" suffix-icon="el-icon-edit" v-model="editText" />
<el-input placeholder="Clearable input" clearable v-model="clearText" />
<el-input placeholder="Disabled input" disabled v-model="disabledText" />
</template>
<script>
export default {
data() {
return {
searchText: '',
editText: '',
clearText: '',
disabledText: 'Cannot be edited',
};
},
};
</script>
clearable attribute lets users clear the input with a single click.🎯 Select
A select lets users choose from a list of options. Element UI supports both single and multiple selection.
Single and Multiple Selection
Use the multiple attribute to switch between single and multiple selection.
<template>
<el-select v-model="selected" placeholder="Select an option">
<el-option label="Apple" value="apple"></el-option>
<el-option label="Banana" value="banana"></el-option>
<el-option label="Orange" value="orange"></el-option>
</el-select>
<el-select v-model="multipleSelected" multiple placeholder="Select options">
<el-option label="Apple" value="apple"></el-option>
<el-option label="Banana" value="banana"></el-option>
<el-option label="Orange" value="orange"></el-option>
</el-select>
</template>
<script>
export default {
data() {
return {
selected: '',
multipleSelected: [],
};
},
};
</script>
Grouping and Disabling Options
You can group options and disable individual choices.
<template>
<el-select v-model="groupSelected" placeholder="Grouped options">
<el-option-group label="Fruit">
<el-option label="Apple" value="apple"></el-option>
<el-option label="Banana" value="banana" disabled></el-option>
</el-option-group>
<el-option-group label="Vegetables">
<el-option label="Tomato" value="tomato"></el-option>
<el-option label="Cucumber" value="cucumber"></el-option>
</el-option-group>
</el-select>
</template>
<script>
export default {
data() {
return {
groupSelected: '',
};
},
};
</script>
📅 DatePicker
Date pickers let users choose dates or times. Element UI supports individual dates, date ranges, and time selection.
Dates and Date Ranges
<template>
<el-date-picker v-model="date" type="date" placeholder="Select a date" />
<el-date-picker
v-model="dateRange"
type="daterange"
start-placeholder="Start date"
end-placeholder="End date"
/>
</template>
<script>
export default {
data() {
return {
date: '',
dateRange: [],
};
},
};
</script>
Controlling Date Formats
Use format and value-format to control the displayed date format and the bound value’s format.
<template>
<el-date-picker
v-model="formattedDate"
type="date"
placeholder="Formatted date"
format="yyyy-MM-dd"
value-format="yyyy-MM-dd"
></el-date-picker>
</template>
<script>
export default {
data() {
return {
formattedDate: '',
};
},
};
</script>
format controls what users see; value-format controls the format stored in your data, which is useful when working with backend APIs.📝 Form
Forms collect user data. Element UI’s Form component provides a range of supporting features.
el-form and el-form-item
el-form is the form container, and el-form-item wraps each control. The label attribute provides the field’s label.
<template>
<el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
<el-form-item label="Username" prop="username">
<el-input v-model="form.username"></el-input>
</el-form-item>
<el-form-item label="Email" prop="email">
<el-input v-model="form.email"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm">Submit</el-button>
<el-button @click="resetForm">Reset</el-button>
</el-form-item>
</el-form>
</template>
<script>
export default {
data() {
return {
form: {
username: '',
email: '',
},
rules: {
username: [
{ required: true, message: 'Enter a username', trigger: 'blur' },
{ min: 3, max: 12, message: 'Use 3 to 12 characters', trigger: 'blur' },
],
email: [
{ required: true, message: 'Enter an email address', trigger: 'blur' },
{ type: 'email', message: 'Enter a valid email address', trigger: ['blur', 'change'] },
],
},
};
},
methods: {
submitForm() {
this.$refs.formRef.validate(valid => {
if (valid) {
alert('Submitted successfully!');
} else {
console.log('Validation failed');
return false;
}
});
},
resetForm() {
this.$refs.formRef.resetFields();
},
},
};
</script>
Automatic Label Width and Label Placement
Set label-width to specify label width, or use auto to size labels automatically.
Set label-position to left, right, or top to control label placement.
<el-form label-width="auto" label-position="top">
<!-- Form content -->
</el-form>
label-width to improve alignment and combine it with label-position for different layouts.💬 Message / MessageBox / Notification
Element UI provides several components for giving users feedback about their actions.
Message
A brief feedback message that disappears automatically.
this.$message({
message: 'Operation successful',
type: 'success',
});
MessageBox
A dialog that requires user confirmation or input.
this.$confirm('Are you sure you want to delete this?', 'Confirmation', {
confirmButtonText: 'Confirm',
cancelButtonText: 'Cancel',
type: 'warning',
})
.then(() => {
this.$message({
type: 'success',
message: 'Deleted successfully!',
});
})
.catch(() => {
this.$message({
type: 'info',
message: 'Deletion canceled',
});
});
Notification
A notification usually appears in a page corner without interrupting the user’s work.
this.$notify({
title: 'Notification title',
message: 'This is a notification',
type: 'info',
});
❓ Questions and Answers
Q1: How do I show a loading state on an Element UI button?
Add :loading="true" to el-button, or bind loading to reactive data to control it dynamically.
Q2: How do I add a prefix icon to an input?
Set prefix-icon to an Element UI icon class name.
Q3: What happens when form validation fails?
Error messages appear below the form fields, and the submission handler should stop submission so users can correct their input.
🧪 Exercises
Exercise 1
Build an Element UI login form with username and password inputs. Validate that both fields are filled in before submitting.
<template>
<el-form :model="loginForm" :rules="rules" ref="loginRef" label-width="80px">
<el-form-item label="Username" prop="username">
<el-input v-model="loginForm.username"></el-input>
</el-form-item>
<el-form-item label="Password" prop="password">
<el-input type="password" v-model="loginForm.password"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitLogin">Log in</el-button>
</el-form-item>
</el-form>
</template>
<script>
export default {
data() {
return {
loginForm: {
username: '',
password: '',
},
rules: {
// Add validation rules
},
};
},
methods: {
submitLogin() {
// Add submission logic
},
},
};
</script>
Exercise 2
Create a multiple select with a Fruit option group, disable Banana, and display the selected values.
<template>
<el-select v-model="fruits" multiple placeholder="Select fruit">
<el-option-group label="Fruit">
<!-- Write your code here -->
</el-option-group>
</el-select>
<p>Selected fruit: {{ fruits.join(', ') }}</p>
</template>
<script>
export default {
data() {
return {
fruits: [],
};
},
};
</script>
🎉 You now know the basics of Element UI buttons, inputs, selects, date pickers, forms, and message components. Try them in your project!
