From 079b921392913518c04f87c848cce6cf35531b04 Mon Sep 17 00:00:00 2001 From: Hooray Hu <304327508@qq.com> Date: Sat, 6 Jan 2024 22:37:18 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=20`defineModel`=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E9=83=A8=E5=88=86=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/ImageUpload/index.vue | 11 ++++----- src/components/ImagesUpload/index.vue | 21 +++++++--------- src/components/PcasCascader/index.vue | 24 +++++++------------ src/components/SearchBar/index.vue | 22 ++++++----------- src/layouts/ui-kit/HCheckList.vue | 10 ++++---- src/layouts/ui-kit/HDialog.vue | 18 ++++---------- src/layouts/ui-kit/HInput.vue | 11 ++------- src/layouts/ui-kit/HSelect.vue | 9 +++---- src/layouts/ui-kit/HSlideover.vue | 16 ++++--------- src/layouts/ui-kit/HTabList.vue | 14 +++++------ src/layouts/ui-kit/HToggle.vue | 16 ++----------- .../searchbar.demo.vue | 22 +++++++---------- .../component_extend_example/upload.demo.vue | 4 ++-- 13 files changed, 68 insertions(+), 130 deletions(-) diff --git a/src/components/ImageUpload/index.vue b/src/components/ImageUpload/index.vue index 2d39816e6..a536aeeff 100755 --- a/src/components/ImageUpload/index.vue +++ b/src/components/ImageUpload/index.vue @@ -12,7 +12,6 @@ const props = withDefaults( headers?: UploadProps['headers'] data?: UploadProps['data'] name?: UploadProps['name'] - url?: string size?: number width?: number height?: number @@ -22,7 +21,6 @@ const props = withDefaults( }>(), { name: 'file', - url: '', size: 2, width: 150, height: 150, @@ -33,14 +31,15 @@ const props = withDefaults( ) const emits = defineEmits<{ - 'update:url': [ - url: string, - ] 'onSuccess': [ res: any, ] }>() +const url = defineModel({ + default: '', +}) + const uploadData = ref({ imageViewerVisible: false, progress: { @@ -59,7 +58,7 @@ function previewClose() { } // 移除 function remove() { - emits('update:url', '') + url.value = '' } const beforeUpload: UploadProps['beforeUpload'] = (file) => { const fileName = file.name.split('.') diff --git a/src/components/ImagesUpload/index.vue b/src/components/ImagesUpload/index.vue index 1260a9d50..c538bbf6f 100755 --- a/src/components/ImagesUpload/index.vue +++ b/src/components/ImagesUpload/index.vue @@ -12,7 +12,6 @@ const props = withDefaults( headers?: UploadProps['headers'] data?: UploadProps['data'] name?: UploadProps['name'] - url?: string[] size?: number max?: number width?: number @@ -23,7 +22,6 @@ const props = withDefaults( }>(), { name: 'file', - url: () => [], size: 2, max: 3, width: 150, @@ -35,14 +33,15 @@ const props = withDefaults( ) const emits = defineEmits<{ - 'update:url': [ - url: string[], - ] 'onSuccess': [ res: any, ] }>() +const url = defineModel({ + default: [], +}) + const uploadData = ref({ dialogImageIndex: 0, imageViewerVisible: false, @@ -63,20 +62,16 @@ function previewClose() { } // 移除 function remove(index: number) { - const url = props.url - url.splice(index, 1) - emits('update:url', url) + url.value.splice(index, 1) } // 移动 function move(index: number, type: 'left' | 'right') { - const url = props.url if (type === 'left' && index !== 0) { - url[index] = url.splice(index - 1, 1, url[index])[0] + url.value[index] = url.value.splice(index - 1, 1, url.value[index])[0] } - if (type === 'right' && index !== url.length - 1) { - url[index] = url.splice(index + 1, 1, url[index])[0] + if (type === 'right' && index !== url.value.length - 1) { + url.value[index] = url.value.splice(index + 1, 1, url.value[index])[0] } - emits('update:url', url) } const beforeUpload: UploadProps['beforeUpload'] = (file) => { diff --git a/src/components/PcasCascader/index.vue b/src/components/PcasCascader/index.vue index 9030f50ef..a01b8be37 100755 --- a/src/components/PcasCascader/index.vue +++ b/src/components/PcasCascader/index.vue @@ -8,10 +8,6 @@ defineOptions({ const props = withDefaults( defineProps<{ - modelValue: string[] | { - code: string - name: string - }[] disabled?: boolean type?: 'pc' | 'pca' | 'pcas' format?: 'code' | 'name' | 'both' @@ -23,14 +19,12 @@ const props = withDefaults( }, ) -const emits = defineEmits<{ - 'update:modelValue': [ - value: string[] | { - code: string - name: string - }[], - ] -}>() +const value = defineModel({ + default: [], +}) interface pcasItem { code: string @@ -88,11 +82,11 @@ const pcasData = computed(() => { const myValue = computed({ // 将入参数据转成 code 码 get: () => { - return anyToCode(props.modelValue) + return anyToCode(value.value) }, // 将 code 码转成出参数据 - set: (value) => { - emits('update:modelValue', value ? codeToAny(value) : []) + set: (val) => { + value.value = val ? codeToAny(val) : [] }, }) diff --git a/src/components/SearchBar/index.vue b/src/components/SearchBar/index.vue index 99e70abfb..5a2abe363 100755 --- a/src/components/SearchBar/index.vue +++ b/src/components/SearchBar/index.vue @@ -3,38 +3,30 @@ defineOptions({ name: 'SearchBar', }) -const props = withDefaults( +withDefaults( defineProps<{ - fold?: boolean showToggle?: boolean background?: boolean }>(), { - fold: true, showToggle: true, background: false, }, ) const emits = defineEmits<{ - 'update:fold': [ - value: boolean, - ] 'toggle': [ value: boolean, ] }>() -const isFold = ref(props.fold) - -watch(() => props.fold, (value) => { - isFold.value = value - emits('update:fold', value) +const fold = defineModel('fold', { + default: true, }) function toggle() { - isFold.value = !isFold.value - emits('toggle', isFold.value) + fold.value = !fold.value + emits('toggle', fold.value) } @@ -45,10 +37,10 @@ function toggle() { 'px-4 bg-[var(--g-bg)] transition': background, }" > - +
diff --git a/src/layouts/ui-kit/HCheckList.vue b/src/layouts/ui-kit/HCheckList.vue index e99c74410..6292f1d4e 100644 --- a/src/layouts/ui-kit/HCheckList.vue +++ b/src/layouts/ui-kit/HCheckList.vue @@ -1,7 +1,6 @@