import { reactive, ref } from 'vue'
import { resetObjToPrimitiveType } from '@/utils/tool'
export const usePage = (opts) => {
const {
searchForm = {},
getListApi,
customQueryParameters = () => {},
getListFunc = (opts) => {},
resetFunc = () => {},
sizeChangeFunc = () => {},
currentChangeFunc = () => {}
} = opts
const reset = () => {
Object.assign(searchForm, resetObjToPrimitiveType(searchForm))
resetFunc()
handleCurrentChange(1)
}
const page = reactive({
pageSize: 10,
pageNo: 1,
total: 0
})
const tableData = ref([])
const getList = () => {
const opts = {
...page,
...searchForm,
...customQueryParameters()
}
getListApi(opts).then((res) => {
if (res.code === 0) {
tableData.value = res.data?.rows || []
page.total = res.data?.total || 0
getListFunc(opts)
}
})
}
const handleSizeChange = (size) => {
page.pageSize = size
sizeChangeFunc()
getList()
}
const handleCurrentChange = (cur) => {
page.pageNo = cur
currentChangeFunc()
getList()
}
return {
searchForm,
reset,
page,
tableData,
handleSizeChange,
handleCurrentChange
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
组件内使用
import { reactive, ref, computed } from 'vue'
import { usePage } from '@/composables/usePage'
import testModel from '@/model/test'
const searchForm = reactive({
createEndTime: '',
createStartTime: ''
})
const { reset, page, tableData, handleSizeChange, handleCurrentChange } = usePage({
searchForm,
getListApi: testModel.getList
})
reset()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
store
import {useStore} from 'vuex'
import {computed} from 'vue'
export const useTest = () => {
const store = useStore()
const getOrgById = (id) => {
const orgObj = computed(() => store.state.orgObj)
return orgObj.value[id]
}
return {
getOrgById
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19