以下兩個代碼片段都使用 Vue3 組合 API 正確遞增計數器。我希望方法 1 通過執行
count.value . 但是為什么方法 2this.count 有效呢?是否有過我們想要使用方法 2 的情況?
方法一:
<template>
<button @click="inc">{{ count }}</button>
</template>
<script>
import { ref } from "vue";
export default {
setup() {
const count = ref(0);
const inc = function () {
count.value ;
};
return {
inc,
count,
};
},
};
方法二:
<template>
<button @click="inc">{{ count }}</button>
</template>
<script>
import { ref } from "vue";
export default {
setup() {
const count = ref(0);
const inc = function () {
this.count ;
};
return {
inc,
count,
};
},
};
</script>
uj5u.com熱心網友回復:
我們從來沒有this在setup()!
https://v3.vuejs.org/guide/composition-api-setup.html#usage-of-this
我假設它setup()像建構式一樣作業,并且您使用的是一個function而不是箭頭函式,因此this可能指向該setup()函式并且count是它的一部分。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/346189.html
