Skip to content

Commit ab98664

Browse files
committed
refactor:优化点选验证码组件的渲染与销毁
1 parent 2777a07 commit ab98664

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

web/src/components/clickCaptcha/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { shortUuid } from '/@/utils/random'
21
import { createVNode, render } from 'vue'
32
import clickCaptchaConstructor from './index.vue'
4-
import type { VNode } from 'vue'
3+
import { shortUuid } from '/@/utils/random'
54

65
interface ClickCaptchaOptions {
76
// 验证码弹窗的自定义class
@@ -21,14 +20,18 @@ interface ClickCaptchaOptions {
2120
* @param options
2221
*/
2322
const clickCaptcha = (uuid: string, callback?: (captchaInfo: string) => void, options: ClickCaptchaOptions = {}) => {
24-
let vnode: VNode | null = createVNode(clickCaptchaConstructor, {
23+
const container = document.createElement('div')
24+
const vnode = createVNode(clickCaptchaConstructor, {
2525
uuid,
2626
callback,
2727
...options,
2828
key: shortUuid(),
29+
onDestroy: () => {
30+
render(null, container)
31+
},
2932
})
30-
render(vnode, document.body)
31-
vnode = null
33+
render(vnode, container)
34+
document.body.appendChild(container.firstElementChild!)
3235
}
3336

3437
export default clickCaptcha

web/src/components/clickCaptcha/index.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
</template>
4040

4141
<script setup lang="ts">
42-
import { reactive, computed } from 'vue'
43-
import { getCaptchaData, checkClickCaptcha } from '/@/api/common'
42+
import { computed, reactive } from 'vue'
43+
import { checkClickCaptcha, getCaptchaData } from '/@/api/common'
4444
import { i18n } from '/@/lang'
4545
4646
interface Props {
@@ -85,6 +85,10 @@ const state: {
8585
},
8686
})
8787
88+
const emits = defineEmits<{
89+
(e: 'destroy'): void
90+
}>()
91+
8892
const load = () => {
8993
state.loading = true
9094
getCaptchaData(props.uuid).then((res) => {
@@ -123,7 +127,7 @@ const onCancelRecord = (index: number) => {
123127
}
124128
125129
const onClose = () => {
126-
document.getElementById(props.uuid)?.remove()
130+
emits('destroy')
127131
}
128132
129133
const captchaBoxTop = computed(() => (state.captcha.height + 200) / 2 + 'px')

0 commit comments

Comments
 (0)