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.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
sbt 2.0: takeoff
eed3si9n
0
160
[JA] Domain Modeling with Vanilla Rails: 「操作」から再考するRailsの設計論
yasaichi
3
440
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
180
エージェントを使い続けるためのメタワーク / Meta-work for keeping using the agent
rkaga
1
190
UnityでSystem.Net.WebSocketsなWebSocketサーバが動かないのでUnity Monoのコードを覗いてみた / about implementing websocket server with unity mono
drumath2237
1
610
[2026-10-14](C)Re:ゼロから始める異世界生活〜始まりの終わりと終わりの始まり〜
tosite
0
110
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
170
re:Inventに行く前に知っておきたい現地参加のノウハウ
nokomoro3
0
440
JIS keyboard support in Omarchy
lef237
0
130
npm との違いを手がかりにGo の依存管理を理解したい
optim
0
810
contenteditable と日本語入力に向き合う
colorful12
0
200
TechTown - Creating Composable Callables in Contemporary C++
rollbear
0
130
Featured
See All Featured
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.7k
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
540
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.6k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
1
630
Chasing Engaging Ingredients in Design
codingconduct
0
360
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
330
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
580
We Are The Robots
honzajavorek
0
400
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
380
Side Projects
sachag
456
43k
Bash Introduction
62gerente
615
220k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
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や 勉強会を開催しています! ご参加お待ちしています! ラクス 中途 フロントエンドエンジニアも募集しています。