fix(Image): load event not triggered in nuxt3

This commit is contained in:
chenjiahan 2022-12-03 20:18:01 +08:00
parent 2c30e7ba68
commit 128972a753

View File

@ -3,6 +3,7 @@ import {
watch, watch,
computed, computed,
nextTick, nextTick,
onMounted,
onBeforeUnmount, onBeforeUnmount,
defineComponent, defineComponent,
getCurrentInstance, getCurrentInstance,
@ -69,7 +70,7 @@ export default defineComponent({
setup(props, { emit, slots }) { setup(props, { emit, slots }) {
const error = ref(false); const error = ref(false);
const loading = ref(true); const loading = ref(true);
const imageRef = ref<HTMLElement>(); const imageRef = ref<HTMLImageElement>();
const { $Lazyload } = getCurrentInstance()!.proxy!; const { $Lazyload } = getCurrentInstance()!.proxy!;
@ -156,7 +157,13 @@ export default defineComponent({
} }
return ( return (
<img src={props.src} onLoad={onLoad} onError={onError} {...attrs} /> <img
ref={imageRef}
src={props.src}
onLoad={onLoad}
onError={onError}
{...attrs}
/>
); );
}; };
@ -191,6 +198,15 @@ export default defineComponent({
}); });
} }
// In nuxt3, the image may not trigger load event,
// so the initial complete state should be checked.
// https://github.com/youzan/vant/issues/11335
onMounted(() => {
if (imageRef.value?.complete) {
onLoad();
}
});
return () => ( return () => (
<div <div
class={bem({ round: props.round, block: props.block })} class={bem({ round: props.round, block: props.block })}