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
Family mrubyの進捗
kishima
1
130
『寄り添うラジオ』をAIで作る 体験価値から逆算した、会話しないUXと品質設計
theoriatec2024
3
180
XP祭りでしか伝わらないフリップネタ #xpjug
murabayashi
0
150
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
250
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
1
260
Building an Out-of-Order CPU
latte72
1
790
巨大モノリシックアプリ モダン化大作戦
ktcryomm
1
1k
速く作れる。その次は、速く確かめられる開発へ 〜AIネイティブ開発を支える、Shift Down〜 / Can build fast. Next, moving to development where we can verify fast.
rkaga
5
3.5k
thread_parallel_with_free-threaded_Python_and_NumPy.pdf
riku_sakamoto
0
350
AWS Step Functions 大規模並列の壁を越える / jaws-sonic-2026-niigata-step-functions
kasacchiful
PRO
1
490
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
230
スマート反転とウェブアクセシビリティ
camiha
0
210
Featured
See All Featured
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
760
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
460
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
940
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
6.2k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
WENDY [Excerpt]
tessaabrams
14
39k
Scaling GitHub
holman
464
140k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
850
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
2
280
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
870
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
330
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や 勉強会を開催しています! ご参加お待ちしています! ラクス 中途 フロントエンドエンジニアも募集しています。