She*_*ukh 2 vuejs3 vue-composition-api
我想做的是在设置完成并且页面安装在组合 API 中后访问方法,但 vue 没有访问该方法
我做错了什么?这是我的代码 -
export default defineComponent({
setup() {
onMounted(() => {
this.calcStartIndex();
});
return {};
},
methods: {
calcStartIndex() {
console.log("startIndex");
}
},
})
Run Code Online (Sandbox Code Playgroud)
我收到的错误 -
TypeError: Cannot read properties of undefined (reading 'calcStartIndex')
Run Code Online (Sandbox Code Playgroud)
小智 5
您应该在setup函数内声明方法
export default defineComponent({
setup() {
function calcStartIndex() {
console.log("startIndex");
}
onMounted(() => {
calcStartIndex();
});
return {};
},
})
Run Code Online (Sandbox Code Playgroud)
或者更好地使用script setup
<script setup>
function calcStartIndex() {
console.log("startIndex");
}
onMounted(() => {
calcStartIndex();
});
})
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1252 次 |
| 最近记录: |