Vue3中的element-plus表格實(shí)現(xiàn)代碼
一、element-plus
1.用組件屬性實(shí)現(xiàn)跳轉(zhuǎn)路由

<el-menu
active-text-color="#ffd04b"
background-color="#232323"
:default-active="$route.path" //高亮
text-color="#fff"
router>
<el-menu-item index="/article/channel">
<el-icon>
<Management />
</el-icon>
<span>文章分類</span>
</el-menu-item>2. el-card 組件

<template>
<el-card class="page-container">
<template #header>
<div class="header">
<span>文章分類</span>
<div class="extra">
<el-button type="primary">添加分類</el-button>
</div>
</div>
</template>
...
</el-card>
</template>
<style lang="scss" scoped>
.page-container {
min-height: 100%;
box-sizing: border-box;
.header {
display: flex;
align-items: center;
justify-content: space-between;
}
}
</style>考慮到多個(gè)頁(yè)面復(fù)用,封裝成組件
- props 定制標(biāo)題
- 默認(rèn)插槽 default 定制內(nèi)容主體
- 具名插槽 extra 定制頭部右側(cè)額外的按鈕
- <slot>
<script setup>
defineProps({
title: {
required: true,
type: String
}
})
</script>
<template>
<el-card class="page-container">
<template #header>
<div class="header">
<span>{{ title }}</span>
<div class="extra">
<slot name="extra"></slot>
</div>
</div>
</template>
<slot></slot>
</el-card>
</template>頁(yè)面內(nèi)使用
<template>
<page-container title="文章分類">
<template #extra>
<el-button type="primary"> 添加分類 </el-button>
</template>
主體部分
</page-container>
</template>3.el-表格(重要)
第一步先調(diào)通借口返回?cái)?shù)據(jù)

第二步封裝組件


詳情看官網(wǎng)
<el-table :data="channelList" style="width: 100%">
<el-table-column label="序號(hào)" width="100" type="index"> </el-table-column>
<el-table-column label="分類名稱" prop="cate_name"></el-table-column>
<el-table-column label="分類別名" prop="cate_alias"></el-table-column>
<el-table-column label="操作" width="100">
<template #default="{ row }">
<el-button
:icon="Edit"
circle
plain
type="primary"
@click="onEditChannel(row)"
></el-button>
<el-button
:icon="Delete"
circle
plain
type="danger"
@click="onDelChannel(row)"
></el-button>
</template>
</el-table-column>
<template #empty>
<el-empty description="沒(méi)有數(shù)據(jù)" />
</template>
</el-table>
const onEditChannel = (row) => {
console.log(row)
}
const onDelChannel = (row) => {
console.log(row)
}
4.封裝彈層
<script setup>
import { ref } from 'vue'
const dialogVisible = ref(false)
const open = async (row) => {
dialogVisible.value = true
console.log(row)
}
defineExpose({
open
})
</script>
<template>
<el-dialog v-model="dialogVisible" title="添加彈層" width="30%">
<div>我是內(nèi)容部分</div>
<template #footer>
<span class="dialog-footer">
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary"> 確認(rèn) </el-button>
</span>
</template>
</el-dialog>
</template>

二、封裝公共組件,下拉菜單
1.新建 article/components/ChannelSelect.vue
<template>
<el-select>
<el-option label="新聞" value="新聞"></el-option>
<el-option label="體育" value="體育"></el-option>
</el-select>
</template>2.頁(yè)面中導(dǎo)入渲染
import ChannelSelect from './components/ChannelSelect.vue' <el-form-item label="文章分類:"> <channel-select></channel-select> </el-form-item>
3.調(diào)用接口,動(dòng)態(tài)渲染下拉分類,設(shè)計(jì)成 v-model 的使用方式
<script setup>
import { artGetChannelsService } from '@/api/article'
import { ref } from 'vue'
defineProps({//子組件接收
modelValue: {
type: [Number, String]
}
})
const emit = defineEmits(['update:modelValue'])//提交父組件方法
const channelList = ref([])//定義數(shù)組動(dòng)態(tài)渲染
const getChannelList = async () => {
const res = await artGetChannelsService()
channelList.value = res.data.data
}
getChannelList()
</script>
<template>
<el-select//拆解方法
:modelValue="modelValue"
@update:modelValue="emit('update:modelValue', $event)"
>
<el-option//動(dòng)態(tài)渲染
v-for="channel in channelList"
:key="channel.id"
:label="channel.cate_name"
:value="channel.id"
></el-option>
</el-select>
</template>注意:這里一定要使用大駝峰命名,不然會(huì)報(bào)錯(cuò)
4.父組件定義參數(shù)綁定
const params = ref({//父組件定義數(shù)據(jù)
pagenum: 1,
pagesize: 5,
cate_id: '',
state: ''
})
<channel-select v-model="params.cate_id"></channel-select>//拆分開就是子組件的updata方法vue2和Vue3v-model區(qū)別
- 在Vue 2和Vue 3中,
v-model的使用和行為大體上是相似的,都是用來(lái)創(chuàng)建表單輸入和應(yīng)用狀態(tài)之間的雙向數(shù)據(jù)綁定。不過(guò),隨著Vue 3的推出,有一些細(xì)微的差別和改進(jìn):
Vue 2 中的 v-model
- 基于對(duì)象屬性:Vue 2 使用
v-model實(shí)現(xiàn)雙向綁定時(shí),實(shí)際上是在使用value屬性和input事件的一個(gè)語(yǔ)法糖。 - 組件實(shí)現(xiàn):自定義組件需要定義
value屬性和input事件來(lái)配合v-model的工作。 model選項(xiàng):在Vue 2的自定義組件中,可以使用model選項(xiàng)來(lái)指定一個(gè)自定義的事件,而不是默認(rèn)的input事件。
Vue 3 中的 v-model
- 基于
createModel函數(shù):Vue 3 引入了一個(gè)新的createModel函數(shù),它允許更靈活的雙向綁定實(shí)現(xiàn)。 - 組件實(shí)現(xiàn)改進(jìn):自定義組件可以使用
modelValue屬性和update:modelValue事件來(lái)代替Vue 2中的value和input。 - 模板中的變化:在Vue 3的模板中,應(yīng)該使用
update:modelValue來(lái)監(jiān)聽更新事件,如上一個(gè)警告信息所述,這要求使用全小寫并用連字符分隔的事件名。 - 性能優(yōu)化:Vue 3中的
v-model可以利用Vue 3的性能優(yōu)化,例如通過(guò)避免不必要的渲染來(lái)提高效率。
共同點(diǎn)
- 在兩種版本中,
v-model都是用于創(chuàng)建用戶輸入和應(yīng)用狀態(tài)之間的同步。 - 它們都允許開發(fā)者在模板中以一種簡(jiǎn)潔的方式處理表單輸入和應(yīng)用狀態(tài)的綁定。
遷移注意事項(xiàng)
如果你正在從Vue 2遷移到Vue 3,需要注意以下幾點(diǎn):
- 確保在Vue 3中將
v-model的更新事件監(jiān)聽器更改為使用全小寫的連字符命名法,如update:modelValue。 - 自定義組件可能需要更新以使用新的
modelValue屬性和update:modelValue事件。 - 利用Vue 3的組合式API(如
ref,reactive)來(lái)更好地管理響應(yīng)式狀態(tài)。
總的來(lái)說(shuō),v-model 在Vue 2和Vue 3中的核心概念保持不變,但Vue 3對(duì)其進(jìn)行了一些現(xiàn)代化的改進(jìn)和優(yōu)化。

三、el-表格(進(jìn)階)
1.封裝格式化日期工具函數(shù)
1.新建 utils/format.js 封裝格式化日期函數(shù)
import { dayjs } from 'element-plus'
export const formatTime = (time) => dayjs(time).format('YYYY年MM月DD日')2.導(dǎo)入使用 import { formatTime } from '@/utils/format'
注意這里要用插槽,
<el-table-column label="發(fā)表時(shí)間" prop="pub_date">
<template #default="{ row }">
{{ formatTime(row.pub_date) }}
</template>
</el-table-column>2.分頁(yè)渲染 [element-plus 分頁(yè)]
1.分頁(yè)組件


<el-pagination v-model:current-page="params.pagenum" v-model:page-size="params.pagesize" :page-sizes="[2, 3, 4, 5, 10]" layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange" @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />
const onSizeChange = (size) => {
params.value.pagenum = 1
params.value.pagesize = size
getArticleList()
}
const onCurrentChange = (page) => {
params.value.pagenum = page
getArticleList()
}2.提供分頁(yè)修改邏輯

loading效果

到此這篇關(guān)于Vue3中的element-plus表格的文章就介紹到這了,更多相關(guān)Vue3 element-plus表格內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue3?element-plus?實(shí)現(xiàn)表格數(shù)據(jù)更改功能詳細(xì)步驟
- Vue3實(shí)現(xiàn)Element Plus表格的多選功能與條件操作
- 基于Vue3和Element Plus實(shí)現(xiàn)可擴(kuò)展的表格組件
- vue3基于elementplus 簡(jiǎn)單實(shí)現(xiàn)表格二次封裝過(guò)程
- Vue3?使用Element?Plus表格單選帶checkbox功能
- vue3+element?Plus實(shí)現(xiàn)表格前端分頁(yè)完整示例
- element-plus+Vue3實(shí)現(xiàn)表格數(shù)據(jù)動(dòng)態(tài)渲染
- vue3 + ElementPlus 封裝列表表格組件包含分頁(yè)
- Vue3+Element Plus 通用表格組件封裝與使用實(shí)踐
相關(guān)文章
vue.js學(xué)習(xí)筆記之v-bind和v-on解析
這篇文章主要介紹了vue.js學(xué)習(xí)筆記之v-bind和v-on解析,v-bind 指令用于響應(yīng)地更新 HTML 特征,v-on 指令用于監(jiān)聽DOM事件,文中還給大家提到了v-bind,v-on的縮寫,感興趣的朋友參考下吧2018-05-05
Vue.set()實(shí)現(xiàn)數(shù)據(jù)動(dòng)態(tài)響應(yīng)的方法
這篇文章主要介紹了Vue.set()實(shí)現(xiàn)數(shù)據(jù)動(dòng)態(tài)響應(yīng)的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-02-02
vue使用socket與服務(wù)端進(jìn)行通信的代碼詳解
這篇文章主要給大家介紹了vue如何使用socket與服務(wù)端進(jìn)行通信的相關(guān)資料,在Vue中我們可以將Websocket類封裝成一個(gè)Vue插件,以便全局使用,需要的朋友可以參考下2023-09-09
Vue實(shí)現(xiàn)淘寶購(gòu)物車三級(jí)選中功能詳解
這篇文章主要介紹了通過(guò)Vue實(shí)現(xiàn)淘寶購(gòu)物車中三級(jí)選中的功能,文中的實(shí)現(xiàn)過(guò)程講解詳細(xì),對(duì)我們學(xué)習(xí)Vue有一定的幫助,感興趣的可以了解一下2022-01-01
vue項(xiàng)目初始化過(guò)程中錯(cuò)誤總結(jié)
在Vue.js項(xiàng)目初始化和構(gòu)建過(guò)程中,可能會(huì)遇到多種問(wèn)題,首先,npm?install過(guò)程中報(bào)錯(cuò),如提示“No?such?file?or?directory”,建議刪除package-lock.json文件后重新安裝,在build或run時(shí),若出現(xiàn)core-js相關(guān)錯(cuò)誤2024-09-09

