Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Options APIの課題とComposition APIが解決したこと
Search
Yuta Inoue
May 11, 2025
Programming
65
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Options APIの課題とComposition APIが解決したこと
Yuta Inoue
May 11, 2025
Other Decks in Programming
See All in Programming
Compiling a real-world CLI tool to native with Static Hermes
tmikov2023
0
120
パズルゲームの作り方 / how to make puzzle games
kaityo256
PRO
2
290
ActionController meets Protocol Buffers
sorah
0
520
Agents on Rails - Rails at Scale 2026
irinanazarova
0
560
Findy - エンジニア向け会社紹介/Findy Company Deck
findyinc
6
400k
なぜCTOを降りてFDEを選んだのか?〜なぜプロダクト企業がFDEで顧客の現場に踏み込むのか〜
gonta
1
210
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
160
AIに “答え“を書かせるのを やめてみた
___akari11
0
120
Railsのためのパスキー実践入門 〜マルチテナントSaaSでの意思決定〜
utakaha
2
540
phperだってパンチカードでコーディングしたい
o0h
PRO
0
330
Rでドレミ/Do-Re-Mi_with_R
florets1
0
130
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
1
1.9k
Featured
See All Featured
Done Done
chrislema
187
17k
Optimising Largest Contentful Paint
csswizardry
37
4k
We Have a Design System, Now What?
morganepeng
55
8.4k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
550
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
Building the Perfect Custom Keyboard
takai
2
910
Ten Tips & Tricks for a 🌱 transition
stuffmc
1
270
Un-Boring Meetings
codingconduct
0
460
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.3k
Music & Morning Musume
bryan
48
7.4k
Transcript
Vue.js Options APIの課題と Composition APIが解決したこと 1
背景 前職でVue2のOptions APIを使っていた Vue3でCompositionAPIが何をもたらしたのかが気になった 2
Options APIとは Vue2から使われている従来のコンポーネントの書き方 data、methods、computedなどのオプションごとにコードを書く export default { data() { return
{ count: 0 } }, methods: { increment() { this.count++ } }, computed: { doubled() { return this.count * 2 } } } 3
Composition APIとは Vue3で導入された新しいコンポーネントの書き方 Vueが提供しているrefなどの関数を使ってコードを書く import { ref, computed } from
'vue' const count = ref(0) const doubled = computed(() => count.value * 2) const increment = () => count.value++ 4
これだけみても何がよくなったかわかりずらい 実際に解決したこと順番にみていく 5
課題1: コードが散在する 同じ機能に関するコードがdata, methods, computedなどに分散 ロジックを理解する時にあちこち探す必要がある 6
解決1: コードをまとめて書ける Composition APIなら関連ロジックをまとめて書ける まとまっているので理解しやすい 7
課題2: 機能の再利用が難しい ユーティリティ関数:状態(optionsAPIでいうdata)の共有ができない ミックスイン:名前の衝突や、どのミックスインが何を提供しているか分かりづらい export default { // どのミックスインが何を提供しているか把握しづらい mixins:
[validationMixin, formMixin, userMixin], methods: { // コンポーネント独自の実装があるとミックスインとの関係が複雑に validateEmail() { /* オーバーライドのロジック */ } } } 8
解決2: 機能の再利用が簡単 コンポーザブル関数として状態とロジックをセットで切り出せる 再利用しやすく、名前の衝突も回避できる import { useValidation } from './useValidation'
... const { value, error, validate : validateUser } = useValidation() 9
課題3: TypeScriptとの相性が悪い Options APIでTypeScriptを使うと型定義が煩雑 推論・型安全が弱い(存在しないプロパティにアクセスしても型エラーにならない) interface ComponentData { user: {
name: string } } interface ComponentMethods { updateUser(name: string): void } export default Vue.extend({ data(): ComponentData { return { user: { name: '' } } }, methods: { updateUser(newName: string) { this.user.name = newName this.user.age++ // 実行時にエラー } } }); 10
解決3: TypeScriptとの相性が良い Composition APIは標準的なTypeScript機能がそのまま使える import { ref } from 'vue'
const user = ref({ name: '' }) const updateUser = (newName: string) => { user.value.name = newName user.value.age++ // 型エラーになる } 11
まとめ CompositionAPIはOptionsAPIの下記の課題を解決した 関連ロジックを直感的にまとめやすくなった 状態とロジックの再利用がよりシンプルに TypeScriptがより自然に使える みなさんも新規ロジックを書くときはCompositionAPIを利用してください! 12
ご清聴ありがとうございました! 13