基礎組件
Button 按鈕
一、基礎用法
<el-button>默認按鈕</el-button>
<el-button type="primary">主要按鈕</el-button>
二、按鈕類型 (type)
類型 | 說明 | 示例代碼 |
---|---|---|
default | 默認按鈕 | <el-button>默認</el-button> |
primary | 主要按鈕(藍色) | <el-button type="primary"> |
success | 成功按鈕(綠色) | <el-button type="success"> |
warning | 警告按鈕(黃色) | <el-button type="warning"> |
danger | 危險按鈕(紅色) | <el-button type="danger"> |
info | 信息按鈕(灰色) | <el-button type="info"> |
text | 文字按鈕 | <el-button type="text"> |
三、核心屬性詳解
-
size - 按鈕尺寸
<el-button size="large">大型</el-button> <el-button size="default">默認</el-button> <el-button size="small">小型</el-button>
-
icon - 圖標按鈕
<el-button icon="el-icon-search">搜索</el-button> <el-button icon="el-icon-edit" circle /> <!-- 圓形圖標按鈕 -->
-
disabled - 禁用狀態
<el-button disabled>不可點擊</el-button>
-
loading - 加載狀態
<el-button :loading="true">加載中</el-button>
-
round - 圓角按鈕
<el-button round>圓角按鈕</el-button>
-
plain - 樸素樣式(無背景色)
<el-button plain>樸素按鈕</el-button>
-
link - 鏈接樣式
<el-button type="primary" link>鏈接按鈕</el-button>
-
circle - 圓形按鈕
<el-button icon="el-icon-plus" circle />
-
autofocus - 自動聚焦
<el-button autofocus>自動聚焦</el-button>
-
native-type - 原生按鈕類型
<el-button native-type="submit">提交表單</el-button> <!-- 可選值: button / submit / reset -->
-
color-自定義按鈕顏色
<le-button type="primary" color="#123456">自定義顏色</el-button>
四、特殊按鈕
-
文字按鈕組
<el-button type="text">首頁</el-button> <el-button type="text" disabled>禁用文字按鈕</el-button>
-
圖標+文字組合
<el-button>下載 <el-icon><Download /></el-icon> </el-button>
-
按鈕組 (el-button-group)
<el-button-group><el-button icon="el-icon-arrow-left">上一頁</el-button><el-button>下一頁 <i class="el-icon-arrow-right"></i></el-button> </el-button-group>
五、完整屬性表
屬性 | 說明 | 類型 | 可選值 | 默認值 |
---|---|---|---|---|
type | 按鈕類型 | string | primary/success/warning/danger/info/text | default |
size | 尺寸 | string | large/default/small | default |
icon | 圖標組件 | Component / string | - | — |
native-type | 原生 type 屬性 | string | button / submit / reset | button |
loading | 加載狀態 | boolean | — | false |
disabled | 禁用狀態 | boolean | — | false |
plain | 樸素樣式 | boolean | — | false |
round | 圓角按鈕 | boolean | — | false |
circle | 圓形按鈕 | boolean | — | false |
link | 鏈接按鈕 | boolean | — | false |
autofocus | 自動聚焦 | boolean | — | false |
loading-icon | 自定義加載圖標 | Component / string | - | Loading |
提示:所有圖標需先導入
@element-plus/icons-vue
,可通過 Element Plus 圖標文檔 查詢可用圖標
Border 邊框
一、基礎使用
在元素上直接添加以下類名即可生效,無需額外導入 CSS
類名 | 作用 |
---|---|
el-border | 添加全方向1px邊框 |
el-border-top | 僅添加上邊框 |
el-border-left | 僅添加左邊框 |
el-border-bottom | 僅添加下邊框 |
el-border-right | 僅添加右邊框 |
el-border-0 | 移除全部邊框 |
示例代碼:
<div class="el-border">全邊框容器</div>
<div class="el-border-top">上邊框標題</div>
二、邊框粗細控制
通過組合類名精確控制邊框粗細:
類名 | 作用 |
---|---|
el-border-base | 默認1px邊框(同.el-border ) |
el-border-thin | 0.5px超細邊框 |
el-border-thick | 2px粗邊框 |
使用示例:
<div class="el-border el-border-thick">粗邊框容器</div>
<div class="el-border-top el-border-base">標準上邊框</div>
三、邊框樣式定制
支持多種邊框樣式,與其他屬性組合使用:
類名 | 作用 |
---|---|
el-border-solid | 實線(默認) |
el-border-dashed | 虛線 |
el-border-dotted | 點狀線 |
el-border-double | 雙實線 |
高級組合示例:
<div class="el-border el-border-dashed el-border-thick">粗虛線邊框</div>
<div class="el-border-bottom el-border-dotted">點狀下劃線</div>
四、邊框顏色控制
使用語義化顏色類名(需激活主題色):
類名 | 顏色說明 |
---|---|
el-border-primary | 主題主色(默認藍) |
el-border-success | 成功綠色 |
el-border-warning | 警告黃色 |
el-border-danger | 危險紅色 |
el-border-info | 信息灰色 |
帶顏色的邊框示例:
<div class="el-border el-border-success">成功提示框</div>
<div class="el-border-top el-border-warning">警告上邊框</div>
💡 提示:在
app.vue
中設置主題色:import { createApp } from 'vue' import ElementPlus from 'element-plus'const app = createApp(App) app.use(ElementPlus, { color: '#ff4500' // 自定義主題色 })
五、圓角控制
完整圓角控制解決方案:
類名 | 作用 |
---|---|
el-border-radius-base | 默認圓角(4px) |
el-border-radius-small | 小圓角(2px) |
el-border-radius-round | 大圓角(20px) |
el-border-radius-circle | 圓形(50%) |
圓角組合示例:
<div class="el-border el-border-radius-base">標準圓角卡片</div>
<button class="el-border el-border-radius-circle">圓形按鈕</div>
Container 布局容器
一、容器組件架構
<el-container> <!-- 根容器:管理整體布局流 --><el-header></el-header> <!-- 頂欄:頁面頭部 --><el-aside></el-aside> <!-- 側欄:導航菜單區 --><el-main></el-main> <!-- 主區:核心內容容器 --><el-footer></el-footer> <!-- 底欄:頁面底部信息 -->
</el-container>
組件特性:自動實現 Flex 彈性布局,無需手動編寫 flex 屬性
二、核心組件 API 詳解
1. <el-container>
根容器
參數 | 類型 | 默認值 | 說明 |
---|---|---|---|
direction | vertical/horizontal | horizontal | 子元素的排列方向 |
事件 | 說明 | ||
@scroll | 容器滾動時觸發 |
2. <el-header>
頂欄
參數 | 類型 | 默認值 | 說明 |
---|---|---|---|
height | string/number | 60px | 設置高度(支持任何 CSS 單位) |
插槽 | 說明 | ||
default | 自定義頭部內容 |
3. <el-aside>
側邊欄
參數 | 類型 | 默認值 | 說明 |
---|---|---|---|
width | string/number | 300px | 設置寬度 |
collapse | boolean | false | 折疊狀態 |
事件 | 說明 | ||
@collapse-change | 折疊狀態變化時觸發 |
4. <el-main>
主內容區
特性 | 說明 |
---|---|
自動特性 | 自動填充剩余空間并生成垂直滾動條 |
插槽 | default 區域用于放置頁面核心內容 |
5. <el-footer>
底欄
參數 | 類型 | 默認值 |
---|---|---|
height | string/number | 60px |
三、三種經典布局方案
方案1:上下布局(管理后臺常用)
<el-container direction="vertical"><el-header>系統標題</el-header><el-container><el-aside width="200px">菜單導航</el-aside><el-main>數據看板</el-main></el-container>
</el-container>
方案2:左右布局(文檔類頁面)
<el-container><el-aside width="240px">文檔目錄</el-aside><el-container direction="vertical"><el-header>當前章節標題</el-header><el-main>文檔內容</el-main><el-footer>版權信息</el-footer></el-container>
</el-container>
方案3:頂部主導航+二級側欄
<el-container direction="vertical"><el-header>主導航欄</el-header><el-container><el-aside width="180px">二級導航</el-aside><el-container direction="vertical"><el-main>主體內容</el-main><el-footer>操作指引</el-footer></el-container></el-container>
</el-container>
四、高級開發技巧
1. 動態側邊欄(帶折疊動畫)
<script setup>
import { ref } from 'vue'
const isCollapsed = ref(false)
</script><template><el-aside :width="isCollapsed ? '64px' : '240px'"@collapse-change="isCollapsed = $event"><el-button @click="isCollapsed = !isCollapsed">{{ isCollapsed ? '展開' : '折疊' }}</el-button><transition name="el-fade-in-linear"><div v-show="!isCollapsed">導航內容</div></transition></el-aside>
</template>
2. 懸浮頂欄(滾動時固定)
/* 全局樣式 */
.el-header {position: sticky;top: 0;z-index: 1000;box-shadow: 0 2px 12px rgba(0,0,0,0.1);transition: all 0.3s;
}
3. 響應式適配(移動端優化)
<el-aside :width="windowWidth > 768 ? '220px' : '0'"><div v-if="windowWidth > 768">桌面端菜單</div>
</el-aside>
// 監聽窗口變化
import { onMounted, onUnmounted, ref } from 'vue'const windowWidth = ref(window.innerWidth)
const handleResize = () => windowWidth.value = window.innerWidthonMounted(() => window.addEventListener('resize', handleResize))
onUnmounted(() => window.removeEventListener('resize', handleResize))
Layout 布局
一、基礎布局示例
<template><div class="layout-demo"><el-row><el-col :span="24"><div class="grid-content header">24 分欄 - 頭部</div></el-col></el-row><el-row><el-col :span="6"><div class="grid-content sidebar">6 分欄 - 側邊欄</div></el-col><el-col :span="18"><div class="grid-content main">18 分欄 - 內容區</div></el-col></el-row><el-row><el-col :span="8"><div class="grid-content card">8 分欄 - 卡片1</div></el-col><el-col :span="8"><div class="grid-content card">8 分欄 - 卡片2</div></el-col><el-col :span="8"><div class="grid-content card">8 分欄 - 卡片3</div></el-col></el-row><el-row><el-col :span="6"><div class="grid-content footer">6 分欄</div></el-col><el-col :span="6" :offset="6"><div class="grid-content footer">偏移6列</div></el-col><el-col :span="6"><div class="grid-content footer">6 分欄</div></el-col></el-row></div>
</template><style scoped>
.layout-demo {max-width: 1200px;margin: 20px auto;
}.header, .footer {background-color: #3498db;color: white;
}.sidebar {background-color: #2ecc71;color: white;
}.main {background-color: #f1c40f;
}.card {background-color: #e74c3c;color: white;
}.grid-content {border-radius: 4px;min-height: 36px;display: flex;align-items: center;justify-content: center;font-weight: bold;padding: 20px 0;margin-bottom: 10px;
}
</style>
二、核心概念
1. 柵格系統
- 總寬度分為 24 欄
<el-row>
代表行容器<el-col>
代表列容器,可通過span
設置占據列數
2. 關鍵功能
- 間隔: 使用
gutter
設置列間距 - 響應式: 適配多種屏幕尺寸
- 偏移: 使用
offset
設置列偏移量 - 對齊: 支持水平和垂直對齊方式
三、布局方式詳解
1. 基礎分欄布局
<el-row><el-col :span="8">8列(33%)</el-col><el-col :span="8">8列(33%)</el-col><el-col :span="8">8列(33%)</el-col>
</el-row>
2. 設置間隔 (gutter)
<el-row :gutter="20"><el-col :span="6"><div>6列(間隔20px)</div></el-col><el-col :span="6"><div>6列(間隔20px)</div></el-col><el-col :span="6"><div>6列(間隔20px)</div></el-col><el-col :span="6"><div>6列(間隔20px)</div></el-col>
</el-row>
3. 響應式布局
<el-row :gutter="10"><!-- 超小屏幕(手機)12分欄,小屏幕(平板)6分欄,中屏及以上8分欄 --><el-col :xs="12" :sm="6" :md="8" :lg="8" :xl="8"><div>自適應布局</div></el-col><!-- 大屏應用示例 --><el-col :xs="24" :sm="18" :md="16" :lg="14" :xl="12"><div>大屏內容區域</div></el-col>
</el-row>
4. 偏移布局
<el-row><el-col :span="6" :offset="6"><div>偏移6列</div></el-col><el-col :span="6" :offset="6"><div>偏移6列</div></el-col>
</el-row>
5. 對齊方式
<!-- 水平對齊 -->
<el-row justify="center"><el-col :span="6"><div>居中</div></el-col><el-col :span="6"><div>居中</div></col>
</el-row><el-row justify="end"><el-col :span="6"><div>右對齊</div></el-col>
</el-row><!-- 垂直對齊 -->
<el-row align="middle" style="height: 100px;"><el-col :span="6"><div>垂直居中</div></el-col><el-col :span="6"><div style="height: 60px;">高度不一致</div></el-col>
</el-row>
四、完整API詳解
Row(行)屬性
參數 | 說明 | 類型 | 可選值 | 默認值 |
---|---|---|---|---|
gutter | 列間距(單位px) | number | — | 0 |
justify | 水平排列方式 | string | start/end/center/space-around/space-between | start |
align | 垂直排列方式 | string | top/middle/bottom | top |
tag | 自定義HTML標簽 | string | * | div |
Col(列)屬性
參數 | 說明 | 類型 | 可選值 | 默認值 |
---|---|---|---|---|
span | 柵格占位(1-24) | number | — | 24 |
offset | 左側偏移量(列數) | number | — | 0 |
push | 向右移動列數(DOM順序不變) | number | — | 0 |
pull | 向左移動列數(DOM順序不變) | number | — | 0 |
xs | <768px 的響應式設置 | number/object | — | — |
sm | ≥768px 的響應式設置 | number/object | — | — |
md | ≥992px 的響應式設置 | number/object | — | — |
lg | ≥1200px 的響應式設置 | number/object | — | — |
xl | ≥1920px 的響應式設置 | number/object | — | — |
tag | 自定義HTML標簽 | string | * | div |
響應式對象的可選值
<el-col :md="{ span: 8, offset: 2 }">...
</el-col>
響應式配置對象屬性:
span
- 柵格占位offset
- 左側偏移量push
- 向右移動量pull
- 向左移動量