Skip to main content

๐Ÿ‘ฉโ€๐Ÿ’ป ็”Ÿๅ‘ฝ้€ฑๆœŸ

้—œๆ–ผโ€‹

็•ถ่ซ‡ๅˆฐ Vue.js ็š„ Composition API ็”Ÿๅ‘ฝ้€ฑๆœŸๆ™‚๏ผŒๆˆ‘ๅ€‘้œ€่ฆ็†่งฃๅœจไฝฟ็”จ Composition API ๆ™‚๏ผŒๅฆ‚ไฝ•็ฎก็†็ต„ไปถ็š„็”Ÿๅ‘ฝ้€ฑๆœŸไบ‹ไปถ๏ผŒ

Composition API ๆ˜ฏ Vue 3 ไธญๅผ•ๅ…ฅ็š„ไธ€ๅ€‹ๆ–ฐ็š„ API๏ผŒๅฎƒๅ…่จฑๆˆ‘ๅ€‘ๆ›ด้ˆๆดปๅœฐ็ต„็น”ๅ’Œ้‡็”จไปฃ็ขผ๏ผŒไฝ†ๅฎƒ็š„็”Ÿๅ‘ฝ้€ฑๆœŸ็ฎก็†ๆ–นๅผ่ˆ‡ไปฅๅ‰็š„้ธ้ … API ๆœ‰ๆ‰€ไธๅŒ

็”Ÿๅ‘ฝ้€ฑๆœŸโ€‹

่ตทๅง‹้ปžโ€‹

็จ‹ๅผ็š„้€ฒๅ…ฅ้ปžๆœƒๅพž setup ้–‹ๅง‹ๅŸท่กŒ

export default {
setup() {
return {};
},
};

propsโ€‹

ๅœจ Composition API ไธญ๏ผŒๅฏไปฅไฝฟ็”จ props ้–‹ๅง‹่™•็†ๅพž็ˆถ็ดš็ต„ไปถ (ไธŠๅฑค็ต„ไปถ) ๅ‚ณ้ž้Žไพ†็š„ๅฑฌๆ€ง๏ผŒ

้€™ๆ˜ฏ้€š้Ž setup ๅ‡ฝๆ•ธ็š„็ฌฌไธ€ๅ€‹ๅƒๆ•ธไพ†ๅฏฆ็พ็š„

ไพ‹ๅฆ‚๏ผŒๅฆ‚ๆžœ็ต„ไปถๆŽฅๆ”ถไธ€ๅ€‹ๅ็‚บ title ็š„ prop

export default {
props: {
title: String,
},
setup(props) {
// ้€ฒ่กŒ่งฃๆง‹
const {title} = props;

return {
title
};
},
};

data ่ˆ‡ refโ€‹

Composition API ๅผ•ๅ…ฅไบ† ref ๅ‡ฝๆ•ธ๏ผŒ็”จๆ–ผๅ‰ตๅปบๅ…ทๆœ‰้Ÿฟๆ‡‰ๅผ่ƒฝๅŠ›็š„ๆ•ธๆ“š

ๅฏไปฅๅฐ‡ ref ๅ‡ฝๆ•ธ็”จๆ–ผ็ต„ไปถๅ…ง้ƒจ็š„ๆ•ธๆ“š

import { ref } from 'vue';

export default {
setup() {
const count = ref(0);

// ๅœจๆจกๆฟไธญไฝฟ็”จ count.value ไพ†่จชๅ•ๆ•ธๆ“š
return {
count,
};
},
};