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
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.3k
Vue_CLIプロジェクトにViteを導入検討してみた
kitsuneeee
1
8.2k
Vue.js + TypeScriptによる 新規サービス開発の振り返り/frontend-looking-back
kitsuneeee
4
2.2k
Other Decks in Programming
See All in Programming
新人はどこまで自力でやり、どこからAIに頼るべきか/エンジニア育成に向き合う_先輩たちの悩みと知見共有会
toppan_digital_dev
1
590
マイコン向けの軽量Ruby「PicoRuby」で各種デバイスを制御するネイティブアプリの実現手法
bash0c7
0
310
【高い買い物LT会】初任給で話題の国産フィジカルAIを買った話
akagami
PRO
0
130
Seeing Through Serverless: Observability for AWS Lambda with ADOT and CloudWatch Application Signals
seike460
PRO
1
130
数年滞っていたダークモード対応をおよそ2週間で完了させる
chigichan24
0
700
App Storeの外へ──日本のiOSサイドローディング入門 for iOSDC Japan 2026
yuukiw00w
0
130
自分的「カンファレンスの楽しみ方」
syumai
0
200
Deep dive into the select statement (GopherCon UK)
jespino
0
170
週末にAI-DLCを本気で回したら$1,600溶けた
hbashimizu
0
130
信頼性の目標を誰も求めてない
shubox
0
500
Go × SIMDで高速化するベクトル検索 ~ルーフラインモデルでSIMDが効く境界を探れ! ~
po3rin
1
190
標準パッケージに uuid が追加された 背景から見る Go らしい意思決定 / go_127_uuid_decision
convto
5
5.6k
Featured
See All Featured
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
880
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
370
How to Think Like a Performance Engineer
csswizardry
28
2.8k
WENDY [Excerpt]
tessaabrams
13
39k
Context Engineering - Making Every Token Count
addyosmani
9
1.1k
Music & Morning Musume
bryan
47
7.4k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
490
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
400
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
The Pragmatic Product Professional
lauravandoore
37
7.4k
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や 勉強会を開催しています! ご参加お待ちしています! ラクス 中途 フロントエンドエンジニアも募集しています。