Commit 0355c7d238d1c25e00b9fc277aed35a0351f8998
1 parent
3c59915d
分页
Showing
4 changed files
with
204 additions
and
2 deletions
src/components/Pagination/index.vue
0 → 100644
| 1 | +<script setup lang="ts"> | |
| 2 | +import { defineEmits } from "vue"; | |
| 3 | + | |
| 4 | +// 每个属性都必传,不带默认值 | |
| 5 | +// defineProps<{ | |
| 6 | +// total: number; | |
| 7 | +// currentPage: number; | |
| 8 | +// pageSizes: number[]; | |
| 9 | +// pageSize: number; | |
| 10 | +// }>(); | |
| 11 | + | |
| 12 | +// 可以带默认值 | |
| 13 | +interface Props { | |
| 14 | + total: number; | |
| 15 | + pageSizes?: number[]; | |
| 16 | + pageSize?: number; | |
| 17 | + currentPage?: number; | |
| 18 | + tagName?: string | |
| 19 | +} | |
| 20 | +const props = withDefaults(defineProps<Props>(), { | |
| 21 | + total: 0, | |
| 22 | + pageSizes: () => [10, 20, 50, 100], | |
| 23 | + pageSize: 10, | |
| 24 | + currentPage: 1, | |
| 25 | +}); | |
| 26 | + | |
| 27 | + | |
| 28 | +const handleSizeChange = (val) => { | |
| 29 | + emit("pageChange", props.currentPage, val, props.tagName) | |
| 30 | +} | |
| 31 | +const handleCurrentChange = (val) => { | |
| 32 | + emit('pageChange', val, props.pageSize, props.tagName) | |
| 33 | +} | |
| 34 | +const emit= defineEmits<{ | |
| 35 | + (e: 'pageChange', curPage: number, curSize: number, tagName: string): void | |
| 36 | +}>() | |
| 37 | +</script> | |
| 38 | + | |
| 39 | +<template> | |
| 40 | + <el-pagination | |
| 41 | + v-model:currentPage="currentPage" | |
| 42 | + :page-sizes="pageSizes" | |
| 43 | + :page-size="pageSize" | |
| 44 | + layout="total, sizes, prev, pager, next, jumper" | |
| 45 | + :total="total" | |
| 46 | + @size-change="handleSizeChange" | |
| 47 | + @current-change="handleCurrentChange" | |
| 48 | + > | |
| 49 | + </el-pagination> | |
| 50 | +</template> | ... | ... |
src/components/Test/index.vue
0 → 100644
| 1 | +<script setup lang="ts"> | |
| 2 | + import {ref, reactive} from 'vue' | |
| 3 | + const count = ref(100) | |
| 4 | + const obj = reactive({ | |
| 5 | + name: 'Tom', | |
| 6 | + age: 15, | |
| 7 | + addr: 'Beijing', | |
| 8 | + }) | |
| 9 | + interface Props { | |
| 10 | + maxSize?: number, | |
| 11 | + limit?: boolean, | |
| 12 | + collections?: string[], | |
| 13 | + title: string | |
| 14 | + } | |
| 15 | + const props = withDefaults(defineProps<Props>(), { | |
| 16 | + maxSize: 500, | |
| 17 | + limit: false, | |
| 18 | + collections: () => ['邮票', '唱片'], | |
| 19 | + title: '' | |
| 20 | + }); | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + const singleFunc = () => { | |
| 25 | + console.log('组件中的单独一个方法') | |
| 26 | + } | |
| 27 | + const addCount = () => { | |
| 28 | + count.value = count.value + 19 | |
| 29 | + } | |
| 30 | + // 把该组件内的数据、函数等暴露给上级页面 | |
| 31 | + defineExpose({ | |
| 32 | + count, | |
| 33 | + addCount, | |
| 34 | + singleFunc | |
| 35 | + }); | |
| 36 | + | |
| 37 | + const emitFunc = () => { | |
| 38 | + console.log('专门用来emit的方法') | |
| 39 | + emit('emitFunc', count.value) | |
| 40 | + } | |
| 41 | + const emit= defineEmits<{ | |
| 42 | + (e: 'emitFunc', str: number): void, | |
| 43 | + (e: 'update:title', str: number | string): void, | |
| 44 | + }>() | |
| 45 | +</script> | |
| 46 | + | |
| 47 | +<template> | |
| 48 | + <div style="background-color: cyan; padding: 10px;" @click="emitFunc"> | |
| 49 | + <p>这是一个组件的内部</p> | |
| 50 | + <div style="background-color: pink"> | |
| 51 | + <p>粉色div里面展示的是组件内的数据data</p> | |
| 52 | + <p>{{count}}</p> | |
| 53 | + <p>{{obj.name}}</p> | |
| 54 | + <el-button type="primary" @click="singleFunc">组建中单独方法</el-button> | |
| 55 | + </div> | |
| 56 | + | |
| 57 | + <div style="background-color: gold" @click="$emit('update:title', props.title)"> | |
| 58 | + <p>金色div里面展示的是组件内的属性prop</p> | |
| 59 | + <p>{{props.limit}}</p> | |
| 60 | + <p>{{props.collections}}</p> | |
| 61 | + <p>{{props.maxSize}}</p> | |
| 62 | + <p>{{props.title}}</p> | |
| 63 | + <el-input v-model="title" @change="$emit('update:title', $event)"></el-input> | |
| 64 | + </div> | |
| 65 | + </div> | |
| 66 | +</template> | |
| 0 | 67 | \ No newline at end of file | ... | ... |
src/main.ts
| ... | ... | @@ -5,6 +5,7 @@ import promise from 'es6-promise' |
| 5 | 5 | // import store from '@/store/index' |
| 6 | 6 | import ElementPlus from 'element-plus' |
| 7 | 7 | import 'element-plus/dist/index.css' |
| 8 | +import zhCn from 'element-plus/es/locale/lang/zh-cn' // elementUI配置中文环境 | |
| 8 | 9 | import '@/utils/progress' |
| 9 | 10 | import '@/style/reset.scss' |
| 10 | 11 | import { createPinia } from 'pinia' |
| ... | ... | @@ -26,5 +27,7 @@ createApp(App) |
| 26 | 27 | .use(router) |
| 27 | 28 | // .use(store) |
| 28 | 29 | .use(pinia) |
| 29 | -.use(ElementPlus) | |
| 30 | +.use(ElementPlus, { | |
| 31 | + locale: zhCn, | |
| 32 | +}) | |
| 30 | 33 | .mount('#app') |
| 31 | 34 | \ No newline at end of file | ... | ... |
src/views/tengxi/index.vue
| 1 | 1 | <script setup lang="ts"> |
| 2 | + import {ref, onMounted, reactive} from 'vue' | |
| 3 | + import Test from '@/components/Test/index.vue' | |
| 4 | + import Pagination from '@/components/Pagination/index.vue' | |
| 5 | + // import { onMounted, reactive, ref } from 'vue' | |
| 6 | + // const pageSizes = reactive([10, 20, 40, 30]) | |
| 7 | + // const pageSize = ref(5) | |
| 8 | + // const currentPage = ref(1) | |
| 9 | + // const getSizeChange = (size) => { | |
| 10 | + // console.log('size change') | |
| 11 | + // console.log(size) | |
| 12 | + // console.log(pageSize.value) | |
| 13 | + // pageSize.value = size | |
| 14 | + // } | |
| 15 | + // const getCurrentChange = (cur) => { | |
| 16 | + // console.log('cur change') | |
| 17 | + // console.log(cur) | |
| 18 | + // console.log(currentPage.value) | |
| 19 | + // currentPage.value = cur | |
| 20 | + // } | |
| 21 | + | |
| 22 | + // 上级页面和下级组件中的函数、数据交互,组件视图上加ref | |
| 23 | + const testExpose = ref(null) | |
| 24 | + onMounted(() => { | |
| 25 | + console.log(testExpose.value) | |
| 26 | + console.log(testExpose.value.count) | |
| 27 | + testExpose.value.singleFunc() | |
| 28 | + }) | |
| 29 | + const changeCount = () => { | |
| 30 | + testExpose.value.addCount() | |
| 31 | + console.log(testExpose.value.count) | |
| 32 | + } | |
| 33 | + const acceptEmit = (value) => { | |
| 34 | + console.log('页面中监听组件的事件') | |
| 35 | + console.log(value) | |
| 36 | + } | |
| 37 | + const getTitle = (v) => { | |
| 38 | + console.log('新的回调监听') | |
| 39 | + console.log(v) | |
| 40 | + } | |
| 41 | + const data = reactive({ | |
| 42 | + limit: true, | |
| 43 | + maxSize: 10010, | |
| 44 | + collections: ['苹果', '胡椒'], | |
| 45 | + title: '' | |
| 46 | + }) | |
| 47 | + const pageData = reactive({ | |
| 48 | + total: 108, | |
| 49 | + page: 1, | |
| 50 | + pageSize: 10 | |
| 51 | + }) | |
| 52 | + const pageDataStore = reactive({ | |
| 53 | + total: 178, | |
| 54 | + page: 1, | |
| 55 | + pageSize: 10 | |
| 56 | + }) | |
| 57 | + const handlePageChange = (page, size, tagName) => { | |
| 58 | + console.log('分页的回调') | |
| 59 | + console.log(page) | |
| 60 | + console.log(size) | |
| 61 | + console.log(tagName) | |
| 62 | + pageData.page = page | |
| 63 | + pageData.pageSize = size | |
| 64 | + } | |
| 65 | + const test = (page, size, tagName) => { | |
| 66 | + console.log('分页的回调') | |
| 67 | + console.log(page) | |
| 68 | + console.log(size) | |
| 69 | + console.log(tagName) | |
| 70 | + pageDataStore.page = page | |
| 71 | + pageDataStore.pageSize = size | |
| 72 | + } | |
| 2 | 73 | </script> |
| 3 | 74 | |
| 4 | 75 | <template> |
| 5 | - <p>Tengxi Test Page</p> | |
| 76 | + <div style="background-color: lightgreen; padding: 20px;"> | |
| 77 | + <p>这是组件外的上级页面</p> | |
| 78 | + <el-button type="warning" @click="changeCount">改组件里的count</el-button> | |
| 79 | + <!-- <Test ref="testExpose" :maxSize="10086" :limit="true" :collections="['厕所', '阳台']" @emitFunc="acceptEmit"></Test> --> | |
| 80 | + <Test ref="testExpose" v-model:title="data.title" @update:title="getTitle"></Test> | |
| 81 | + <!-- <Test ref="testExpose" :maxSize="data.maxSize" :limit="data.limit" :collections="data.collections" @emitFunc=" | |
| 82 | + acceptEmit"></Test> --> | |
| 83 | + </div> | |
| 84 | + <!-- {{pageSize}} |||| {{currentPage}} --> | |
| 85 | + <!-- {{hello.total}} --> | |
| 86 | + <Pagination tagName="first" :total="pageData.total" :currentPage="pageData.page" :pageSize="pageData.pageSize" @pageChange="handlePageChange"></Pagination> | |
| 87 | + <Pagination tagName="second" :total="pageDataStore.total" :currentPage="pageDataStore.page" :pageSize="pageDataStore.pageSize" @pageChange="test"></Pagination> | |
| 88 | + <!-- <Pagination :total="100" :pageSize="pageSize" :currentPage:update="currentPage" :pageSizes="pageSizes" @handleSizeChange="getSizeChange" @handleCurrentChange="getCurrentChange"></Pagination> --> | |
| 6 | 89 | </template> | ... | ... |