I am using Vue 3 SFC and trying to create a global property on the window object.
However, I am facing an issue where this property seems to be available only during script execution and becomes undefined immediately after.
For example, the following code correctly outputs ‘Hello World’ in the browser console:
<script setup>
import { onMounted} from 'vue'
onMounted(() => {
window._MYMSG = 'Hello World';
console.log(window._MYMSG);
});
</script>
<template>
</template>
But if you type the same line directly in the browser console immediately after executing the script, you get undefined:
console.log(_MYMSG) // returns undefined
It seems like the property exists while the script executes, and gets deleted after. What am I missing?
P.S. The window object in the script is indeed a global window object. If you replace the console.log line with window.alert(window._MYMSG), you will get a proper alert, confirming this is a window object.