๐ŸŽฏ 7.4 ใ‚นใƒŠใƒƒใƒ—ใ‚ทใƒงใƒƒใƒˆใƒ†ใ‚นใƒˆใฎๆ›ธใๆ›ใˆไพ‹

๏ผˆVue 2.7 ใ‚ฏใƒฉใ‚นใ‚ณใƒณใƒใƒผใƒใƒณใƒˆ โ†’ Composition API ๆ›ธใๆ›ใˆๅฎŸไพ‹๏ผ‰


โœ… ใ€่ฆ็‚นใ€‘

โ†’ ใ€Œ่ฆ‹ใŸ็›ฎใ€ใซๆณจ็›ฎใ—ใŸใ‚นใƒŠใƒƒใƒ—ใ‚ทใƒงใƒƒใƒˆ่จญ่จˆใซๅค‰ใ‚ใ‚‹ใ€‚


๐ŸŸข ใ€็งป่กŒๅ‰๏ผˆใ‚ฏใƒฉใ‚นใ‚ณใƒณใƒใƒผใƒใƒณใƒˆๆ™‚ใฎใ‚นใƒŠใƒƒใƒ—ใ‚ทใƒงใƒƒใƒˆ๏ผ‰ใ€‘

<template>
  <p>{{ message }}</p>
</template>

<script lang="ts">
import { Vue, Component } from 'vue-property-decorator';

@Component
export default class Message extends Vue {
  message: string = 'Hello, Vue!';
}
</script>

๐Ÿงช ใ‚ฏใƒฉใ‚นใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใฎใƒ†ใ‚นใƒˆ

import { shallowMount } from '@vue/test-utils';
import Message from '@/components/Message.vue';

describe('Message.vue (ใ‚ฏใƒฉใ‚น)', () => {
  it('ๅˆๆœŸใ‚นใƒŠใƒƒใƒ—ใ‚ทใƒงใƒƒใƒˆ', () => {
    const wrapper = shallowMount(Message);
    expect(wrapper.html()).toMatchSnapshot();
  });
});


๐ŸŸ  ใ€็งป่กŒๅพŒ๏ผˆComposition API๏ผ‰ใ€‘

<template>
  <p>{{ message }}</p>
</template>

<script lang="ts" setup>
import { ref } from 'vue';

const message = ref('Hello, Vue!');
</script>


๐ŸŸฃ ใ€ใƒ†ใ‚นใƒˆๆ›ธใๆ›ใˆๅพŒใ€‘

import { shallowMount } from '@vue/test-utils';
import Message from '@/components/Message.vue';

describe('Message.vue (Composition API)', () => {
  it('ๅˆๆœŸใ‚นใƒŠใƒƒใƒ—ใ‚ทใƒงใƒƒใƒˆ', () => {
    const wrapper = shallowMount(Message);
    expect(wrapper.html()).toMatchSnapshot(); // โ† ๆ›ธใๆ–นใฏๅŒใ˜ใ€ๅ†…้ƒจใฏ้•ใ†
  });
});

โ†’ ่กจ้ขใฎๆ›ธใๆ–นใฏๅค‰ใ‚ใ‚‰ใชใ„ใŒใ€vm ใ‚’ไฝฟใ‚ใš html() ใซไพๅญ˜ใ™ใ‚‹ใฎใŒ้‡่ฆใช้•ใ„ใ€‚