我Element UI使用心得如何让你的前端开发更简单
在编程世界里,Element UI无疑是前端开发者们的老朋友。它提供了一套丰富的组件,让我们可以快速搭建出高质量且美观的用户界面。在我刚入门的时候,Element UI就像一盏明灯,为我指引着前进的方向。
Element UI使用心得:如何让你的前端开发更简单
一、简介
Element UI是一个基于Vue.js 2.0构建的一款强大的UI框架,它包含了各种各样的组件,如Button、Input、Table等,这些都能够帮助我们快速地构建出响应式和易于维护的Web应用程序。
二、安装与配置
首先,我们需要通过npm或yarn来安装Element UI。打开终端,然后输入以下命令:
npm install element-ui --save
或者如果你使用的是yarn,那么命令将会是:
yarn add element-ui --save
接着,在我们的Vue项目中导入并注册Element UI:
import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
这样,我们就完成了基本的配置工作。接下来,就可以在我们的Vue组件中自由地使用这些元素了。
三、常用组件介绍
1. Button按钮
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>
2. Form表单
Form允许我们创建一个可交互性的数据录入区域,以便用户填写信息,并进行提交处理。
<template>
<div class="form">
<h3>登录表单示例:</h3>
<!-- 输入框 -->
<input v-model.trim="$data.username" placeholder="请输入用户名" />
<!-- 密码框 -->
<input v-model.trim="$data.password" placeholder="请输入密码" />
<!-- 提交按鈕 -->
<button @click.stop.prevent='submit'>登陆</button>
</div>
</template>
<script setup lang='ts'>
let username = ''
let password = ''
function submit() {
alert(`username: ${username}, password: ${password}`);
}
export default {
data() {
return {
username,
password,
};
},
};
</script>
<style scoped lang='scss'>
.form{
h3{
text-align:center;
margin-bottom:10px;
}
input, button{
width:100%;
padding:5px;
font-size:16px;
border-radius:4px;
box-sizing:border-box;
transition:.4s ease-in-out all;
}
button:hover{ background-color:#409EFF; }
}
</style>
3. Table数据表格
Table适合展示大量数据,是一种非常实用的工具,可以帮助我们清晰地显示复杂信息。
<table :data-table-data.prop.data|json_ filterable pagination/>
// 表头部分:
<thead><tr><th>姓名<th>年龄<th>地址/th></tr></thead>;
// 数据部分:
<tbody><tr v-for="(item, index) in tableData.data | json_" :key="'row-'+index">
<td>{{ item.name }}</td><td>{{ item.age }}</td><td>{{ item.address }}</td>/t/dd/tr>;
// 分页控制部分:
<div class='pagination' @click.capture.stop.prevent='$emit("updateCurrentPage", $event.target.dataset.pageIndex)'/>
<pagination :total-page-count=tableData.totalPages current-page-index=current-PageIndex/>/p/div>;
<script setup lang='ts'>
let tableData = reactive({
data:[],
totalRecords:'',
totalPages:'',
currentPageIndex:''
});
onMounted(() => {
let apiUrl = "https://api.example.com/data";
fetch(apiUrl)
.then(response => response.json())
.then(data=>{
const {records,total}=data;
this.tableData=data.records.map(record=>({
...record,
createdAt:new Date(record.createdAt),
updatedAt:new Date(record.updatedAt)
}));
this.tableData.totalRecords=total.recordsCount;
this.tableData.totalPages=Math.ceil(this.tableData.totalRecords/this.config.pageSize);
});
});
const config={
pageSize:'10'
};
defineProps(['currentRowIndex']);
watch(
() => props.currentRowIndex,
(newVal, oldVal) => {
if(newVal!==oldVal){
const rowIndex=(newVal*this.config.pageSize)-this.config.pageSize+1;
if(rowIndex<this.tableData.length){
this.$emit('selectRow', rowIndex); // notify parent component that a row has been selected.
}else{
console.log('当前行索引超出了范围');
}
}
});
provide('config', config);
function selectRow(index:number):void{
console.log(`Select Row at Index:${index}`); // This is called when the user selects a row.
}
setup();
export default defineComponent({ name:"example-component", emits:['select-row'] });