<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>
在Vuejs我想呼叫一個在生命周期鉤子<script setup lang="ts">中定義的函式。onMounted雖然,我可以從onMounted定義的函式/方法中呼叫<script setup lang="ts">
控制臺錯誤:
Uncaught TypeError: something is not a function
uj5u.com熱心網友回復:
顧名思義onMounted(),這個生命周期鉤子總是在組件第一次掛載時執行。對于按鈕單擊,您可以直接在 this 之外呼叫方法onMounted()。
<button @click="callSomething">Click</button>
function callSomething() {
// Logic can come directly here.
}
uj5u.com熱心網友回復:
該something函式僅在onMounted回呼范圍內定義,嘗試在其外部定義它以供鉤子和其他函式使用:
<script setup lang="ts">
function callSomething() {
something(); //not working
}
function something() {
console.log("Hello, World");
}
onMounted(() => {
});
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/442210.html
