<template>
<button @click='change'>点我</button>
</template>
<script setup lang="uts">
const change = () => {
console.log(add("1", "2"));
}
function add(x : string, y : string) : string {
let z : string = x + " " + y
return z;
}
</script>

h***@qq.com
- 发布:2025-02-17 15:03
- 更新:2025-02-17 15:17
- 阅读:46



需要把add方法声明调整到使用之前,即:把add方法声明放到change之前。
在uts中,部分平台不支持变量提升
https://doc.dcloud.net.cn/uni-app-x/uts/function.html#%E8%B0%83%E7%94%A8%E9%99%90%E5%88%B6

套马杆的套子 - 没有解决不了的问题,只有解决不完的问题
这么写就可以了
<template>
<button @click="change">点我</button>
</template>
<script setup lang="uts">
// 先定义 add 函数
function add(x: string, y: string): string {
let z: string = x + " " + y;
return z;
}
// 再定义 change 函数
const change = () => {
console.log(add("1", "2"));
};
</script>