Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Chron's avatar
Level 6

Running vanilla js eventlistener inside vue lifecycle hook

I want to have a preloader that appears until all the resources has been loaded.

import { onMounted, ref } from "vue";

const hasLoaded = ref(false);

onMounted(() => {
	window.addEventListener('load', () => {
		hasLoaded = !hasLoaded; 
	});
});				

I just want to ask if this is just fine? Like, I'm not violating rules, semantics, conventions or anything?

0 likes
1 reply
MaverickChan's avatar

vue has load directive , you can use it.

<template>
	<div @load="dosth">
	</div>
</template>
<script setup>
	const dosth = () => {
		//do something...
	}
</script>

Please or to participate in this conversation.