vue3 中的 function a() { const b = 1 return b} 定义了一个名为 a 的函数,并在函数中定义了一个常量 b,并将其初始值设置为 1。该函数返回 b 的值。要使用该函数,可以在组件或者页面中调用 a()。
例如:
<template>
<div>
<p>{{result}}</p>
</div>
</template>
<script>
export default {
data() {
return {
result: 0
}
},
mounted() {
this.result = a()
}
}
function a() {
const b = 1
return b
}
</script>
在组件中调用 a() 函数并将返回值赋值给 result 上面的例子中,在模板中会显示 1。