<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
>Solution :
The something function is defined only in the scope of the onMounted callback, try to define it outside it to be available for the hook and the other function :
<script setup lang="ts">
function callSomething() {
something(); //not working
}
function something() {
console.log("Hello, World");
}
onMounted(() => {
});
</script>