<script setup lang="ts">
function callSomething() {
something(); //not working
}
onMounted(() => {
function something() {
console.log("Hello, World");
}
});
</script>
<template>
<div>
<button @click="callSomething">Click</button>
</div>
</template>
In Vuejs I want to call a function from <script setup lang="ts"> which is defined in onMounted lifecycle hook. Though, I can call function/method from onMounted that defined in <script setup lang="ts">
Error in console:
Uncaught TypeError: something is not a function
onMounted()? As per it's name it called when component mounted. For a button click, you can call a method directly.onMountedlifecycle hook and I had somevanilajscode andeventswith some logic. I needed to call a function, and inside that function I had someeventsreferences. Thats why actually I needed to call method defined inonMounted.let sliders = document.querySelector(".carouselbox");without put this statement insideonMounted,slidersis not executing when it is inside<script setup lang="ts">onlyquerySelectormethod.