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
コンポーネント間のデータやりとりを色々試してみた.pdf
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
hatsune
July 01, 2020
Programming
530
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
コンポーネント間のデータやりとりを色々試してみた.pdf
hatsune
July 01, 2020
More Decks by hatsune
See All by hatsune
フロントエンド_最強ディレクトリ構成
kitsuneeee
1
5.4k
Vue_CLIプロジェクトにViteを導入検討してみた
kitsuneeee
1
8.2k
Vue.js + TypeScriptによる 新規サービス開発の振り返り/frontend-looking-back
kitsuneeee
4
2.3k
Other Decks in Programming
See All in Programming
選挙速報を多くのユーザーへ 届ける Live Activities 設計
hamayokokuririn
0
210
Androidだけじゃない、DroidKaigiで広がった私の技術スタック
akkeylab
0
110
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
1.1k
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
7
2.3k
Turning Architecture into Unit Tests in the AI Era (NSSpain XIV)
steliosf
PRO
1
120
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
20
20k
Unity6 URPによる海底をテーマにしたVJシステムの解説 | Unity Shader 完全に理解した 勉強会
gam0022
0
300
機械に任せるテスト、人が見るテスト ⽣成AIで引き直した、運⽤保守フェーズの線引き
dske104
0
140
Webの地図
yosuke_furukawa
PRO
6
5k
すこし踏み込む CancellationToken
htkym
2
1.6k
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
390
Heart of Swift Concurrency
koher
0
1.1k
Featured
See All Featured
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
Are puppies a ranking factor?
jonoalderson
2
4k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.1k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.9k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
410
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
270
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
290
Optimising Largest Contentful Paint
csswizardry
37
4k
エンジニアに許された特別な時間の終わり
watany
109
250k
Transcript
コンポーネント間のやりとり いろいろ試してみた 株式会社ラクス UI開発課 北嶋 初音
index • 自己紹介 • Vue.jsのコンポーネント間のやりとり(demo) ◦ props ◦ emit ◦
getter, setter ◦ v-model ◦ 配列・オブジェクトの変更 ◦ refs • まとめ
自己紹介:北嶋 初音 • Web業界5年目 • 前職ではバックエンドも触ってた • 今年1月にラクスに入ってからフロントエンド専任 • 新規サービスの開発に参画中
+
Props:親→子へのデータ受け渡し // script部分 public message = "メッセージ"; // template部分 <child
:message="message" message2="message" /> // script部分 @Prop() message!: string; @Prop() message2!: string; Parent.vue Child.vue message = “メッセージ” message2 = “message” ・変数展開したい場合はコロン付き ・リテラルの場合はコロンなし
Props:親→子へのデータ受け渡し // script部分 public message = "メッセージ"; // template部分 <child
:message="message" /> // script部分 @Prop(Number) message!: number; @Prop({default: 'default'}) message2!: string; Parent.vue Child.vue ・型指定も可能 (コンソールエラーを出してくれる) ・デフォルト値が指定可能 message2 = 'default'
Props:親→子へのデータ受け渡し // script部分 public message = "メッセージ"; // template部分 <child
:message="message" message2="message" /> // script部分 @Prop() message!: string; @Prop() message2!: string; public changeMessage() { this.message = "書き換え"; } Parent.vue Child.vue ・子での直接書き換えはNG ・親側で書き換える
emit:親←子 のデータ受け渡し // script部分 public message = "メッセージ"; public changeMessage(val:
string){ this.message = val; } // template部分 <child :message="message" @onChange="changeMessage" /> // script部分 @Prop() message!: string; public changeMessage(){ this.$emit("onChange", "message"); } Parent.vue Child.vue ・イベント経由で親に”message”を送る ・Propの値は親側で変更する val = ”message”
gettter, setter:Prop値を扱うのに便利 // script部分 public changeMessage(val: string){ this.message = val;
} // template部分 <child @onChange="changeMessage" /> // script部分 @Prop() message!: string; get internalMessage (): string { return this.message; } set internalMessage (val: string) { this.$emit("onChange", val); } Parent.vue Child.vue 子ではinternalMessageを触ればOK
v-model:valueとinputイベントの組み合わせ // script部分 public changeMessage(val: string){ this.message = val; }
// template部分 <child :value="message" @input="changeMessage" /> // script部分 @Prop() value!: string; public changeMessage(val: string){ this.$emit("input", val); } Parent.vue Child.vue ・Propのvalueで受け取り ・emitのinputイベントで親に通知
v-model:valueとinputイベントの組み合わせ // template部分 <child v-model="message" /> // script部分 @Prop()
value!: string; public changeMessage(val: string){ this.$emit("input", val); } Parent.vue Child.vue ・Propのvalueで受け取り ・emitのinputイベントで親に通知 messageの変更イベントを書く 必要もない
配列・オブジェクトの変更 // script部分 messages: string[] = ["test1"]; // template部分 <child
:messages="messages" /> @Prop() messages!: string[]; public changeMessage(): void { // コンソールエラー this.messages = ["書き換え"]; // エラーはないけど変更が検知されない this.messages[0] = "書き換え"; // エラーもなく変更も検知される this.messages.push("追加"); } Parent.vue Child.vue ・配列、オブジェクトの中身は子でも 書き換えできてしまう ・変更を検知させるには配列、オブ ジェクトのメソッドを使うこと
refs:子の要素を触る // script部分 public changeMessage(): void { this.$refs.child.message = "test";
this.$refs.child.changeMessage() } // template部分 <child ref="child" /> <div>{{ $refs.child.message }}</div> // script部分 public message = "子のメッセージ"; public changeMessage(): void { this.message = "書き換え"; } Parent.vue Child.vue ・子どもの変数の書き換えは可能 ・子どものメソッドの実行は可能 参照はできない
まとめ • props:親→子、子で値を触ることはできない • emit:親←子、イベント経由で値を渡せる • getter,setter:Prop値が扱いやすくなる • v-model:valueとinputイベントの組み合わせ •
配列:子から中身は触れる、変更はメソッドを利用する • オブジェクト:配列と同様 • refs:親から子の要素を触れる、参照はできない
ラクスでは定期的にMeetupや 勉強会を開催しています! ご参加お待ちしています! ラクス 中途 フロントエンドエンジニアも募集しています。