Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Provide/Inject で TypeScript の恩恵を受ける方法

SAW
September 25, 2024

Provide/Inject で TypeScript の恩恵を受ける方法

Vue 勉強会 in 大阪 の発表資料です。

SAW

September 25, 2024
Tweet

More Decks by SAW

Other Decks in Programming

Transcript

  1. $(whoami) ࢯ໊Ճ౻फҰ࿠ ࡀ  ϋϯυϧωʔϜ4"8 9 چ5XJUUFS !B[VLJ@FBUFS ؔ੢ͷ*5ΤϯδχΞίϛϡχςΟͷ೐΍͔͠୲౰ ࣗশ

     େࡕࡏॅɾѪ஌ग़਎ ಘҙ෼໺8FCΞϓϦέʔγϣϯ։ൃ 7VF -BSBWFM ʮྫྷ΍͠த՚͸͡Ίͨ͠ʯ͸ฉ͘ͷʹ ʮྫྷ΍͠த՚ऴΘΓ·ͨ͠ʯ͸ฉ͔ͳ͍ͷ͸ ͳͥͩΖ͏ ࠓ೔ͷ໎ݴ
  2. 1SPWJEF*OKFDUͷΠϝʔδ Header Layout Root Main Footer ArticleList ArticleEditor provide('user', me)

    inject('user') inject()Մೳͳൣғ  ઌ૆ͷίϯϙʔωϯτͰ provide()͢ΔσʔλΛొ࿥  ࢠଙͷίϯϙʔωϯτͰ inject()ͯ͠σʔλΛऔಘ
  3. InjectionKey<T>ͷར༻ Layout Main ArticleEditor export const userInjectionKey = Symbol() as

    InjectionKey<User>; import { userInjectionKey } from './InjectionKey'; const me: User = { name: 'Jack' }; provide(userInjectionKey, me); import { userInjectionKey } from './InjectionKey'; const me = inject(userInjectionKey); console.log(me?.name); Layout.vue ArticleEditor.vue InjectionKey.ts InjectionKey<User>ʹΩϟετͨ͠ SymbolΛFYQPSU inject()ͷฦ٫ܕ͸ User|undefinedͱͳΔ