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è)頁面復(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>
頁面內(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="沒有數(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.頁面中導(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
的使用和行為大體上是相似的,都是用來創(chuàng)建表單輸入和應(yīng)用狀態(tài)之間的雙向數(shù)據(jù)綁定。不過,隨著Vue 3的推出,有一些細(xì)微的差別和改進(jìn):
Vue 2 中的 v-model
- 基于對(duì)象屬性:Vue 2 使用
v-model
實(shí)現(xiàn)雙向綁定時(shí),實(shí)際上是在使用value
屬性和input
事件的一個(gè)語法糖。 - 組件實(shí)現(xiàn):自定義組件需要定義
value
屬性和input
事件來配合v-model
的工作。 model
選項(xiàng):在Vue 2的自定義組件中,可以使用model
選項(xiàng)來指定一個(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
事件來代替Vue 2中的value
和input
。 - 模板中的變化:在Vue 3的模板中,應(yīng)該使用
update:modelValue
來監(jiān)聽更新事件,如上一個(gè)警告信息所述,這要求使用全小寫并用連字符分隔的事件名。 - 性能優(yōu)化:Vue 3中的
v-model
可以利用Vue 3的性能優(yōu)化,例如通過避免不必要的渲染來提高效率。
共同點(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
)來更好地管理響應(yīng)式狀態(tài)。
總的來說,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.分頁渲染 [element-plus 分頁]
1.分頁組件
<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.提供分頁修改邏輯
loading效果
到此這篇關(guān)于Vue3中的element-plus表格的文章就介紹到這了,更多相關(guān)Vue3 element-plus表格內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue+Echart柱狀圖實(shí)現(xiàn)疫情數(shù)據(jù)統(tǒng)計(jì)
這篇文章主要介紹了在Vue nuxt項(xiàng)目中,如何使用Echart(百度圖表)柱狀圖來實(shí)現(xiàn)疫情數(shù)據(jù)統(tǒng)計(jì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2021-12-12vue中window.addEventListener(‘scroll‘,?xx)失效的解決
這篇文章主要介紹了vue中window.addEventListener(‘scroll‘,?xx)失效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07使用Vite+Vue3+Vant全家桶快速構(gòu)建項(xiàng)目步驟詳解
這篇文章主要為大家介紹了使用Vite+Vue3+Vant全家桶快速構(gòu)建項(xiàng)目步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Vue + Node.js + MongoDB圖片上傳組件實(shí)現(xiàn)圖片預(yù)覽和刪除功能詳解
這篇文章主要介紹了Vue + Node.js + MongoDB圖片上傳組件實(shí)現(xiàn)圖片預(yù)覽和刪除功能,結(jié)合實(shí)例形式詳細(xì)分析了Vue + Node.js + MongoDB基于圖片上傳組件實(shí)現(xiàn)圖片預(yù)覽和刪除功能相關(guān)操作技巧,需要的朋友可以參考下2020-04-04vue使用動(dòng)態(tài)組件手寫Router View實(shí)現(xiàn)示例
這篇文章主要為大家介紹了vue使用動(dòng)態(tài)組件手寫RouterView實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06vue中使用loadsh的debounce防抖函數(shù)問題
這篇文章主要介紹了vue中使用loadsh的debounce防抖函數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11