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
Vuexで覚える状態管理
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
arm4
December 25, 2017
Technology
780
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Vuexで覚える状態管理
arm4
December 25, 2017
More Decks by arm4
See All by arm4
Google Data Studio 101
fromarm4
0
190
5 Points Of Customizing Vuetify
fromarm4
4
1.4k
about abstract component design using slots of Vue.js
fromarm4
4
1.6k
laravel_lt_party_with_mokumoku_3
fromarm4
0
430
Make it happen in realtime with Laravel Echo and Pusher
fromarm4
0
1.1k
Trying to write a code with Laravel+Vue+TypeScript
fromarm4
0
600
Create a Laravel notification via Slack when batch jobs are finished
fromarm4
0
880
solving frontend issues
fromarm4
1
1.8k
Popular Vue.js UI Frameworks in 2019
fromarm4
2
900
Other Decks in Technology
See All in Technology
開発投資の期待値を上げるプロダクトロードマップづくり ~プロダクトエンジニアが越境して事業を伸ばす~
kekekenta
1
180
LTのテーマ どうきめてる?〜5つの型と私のやり方〜
yama3133
1
110
研究開発部の紹介 / Sansan R&D Profile
sansan33
PRO
5
25k
Geolonia の開発現場における AIの活用について
miya0001
0
100
Snowflakeのコスト最適化を支えるアーキテクチャ設計
ktatsuya
1
1.6k
beyond jj: config & tools ecosystem
indirect
0
440
「守り」で活用するオンデバイスLLM 〜写ってはいけないを総力戦で防ぐ〜 / iOSDC Japan 2026
nakamuuu
0
160
OpenTelemetry eBPF Instrumentationの舞台裏 / Behind the Scenes of OpenTelemetry eBPF Instrumentation
ymotongpoo
4
1.3k
Deploying a Full-Stack Bun-Native Framework on Cloudflare Workers
7nohe
0
170
Claude Codeを「使うほど育つ」AI秘書にするノウハウ
minorun365
PRO
27
23k
負債のメタファと2026年 / Debt Metaphor in Agentic Engineering Age 202609 Edition
twada
PRO
6
2.7k
空間オーディオで過去の 自分(ゴースト)と競うランニング 〜HealthKitのルートを足音に変える実装〜
nao_randd
0
220
Featured
See All Featured
SEO for Brand Visibility & Recognition
aleyda
0
4.7k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Bash Introduction
62gerente
615
220k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
700
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
440
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.6k
Paper Plane (Part 1)
katiecoart
PRO
2
11k
Mind Mapping
helmedeiros
1
360
KATA
mclloyd
PRO
35
15k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
520
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Skip the Path - Find Your Career Trail
mkilby
1
230
Transcript
Vuex で覚える状態管理 Vuex で覚える状態管理 arm4
今日、話すこと 今日、話すこと Vuex って何? 既存プロジェクトではこうでした Vuex を使うとこうなるよ Laravel での導入手順 つまり状態管理って何?
その他
Vuex って何? Vuex って何?
と、説明に入るその前に、 コードから見ていきましょう! そうじゃないと説明が難しくて頭に入らないからね!
既存プロジェクトではこうで 既存プロジェクトではこうで した した
親から子へデータを渡す 親から子へデータを渡す 親から子へデータを渡す際はprops といって、親のコ ンポーネントにカスタム属性を設定し、 それを子コンポーネントに設定したprops オプション で受け取るという手法を使います。
親コンポーネントであるProductList から、 子コンポーネントであるProduct にそれぞれの商品デ ータを渡す場合
親コンポーネント 親コンポーネント resources/assets/js/components/ProductList.vue <template> <div id="product-list" class="row"> <div class="col-md-8 col-md-offset-2">
<product v-for="product in productList" :product="product </div> </div> </template> <script> import Product from './Product.vue' export default { components: { Product, },
子コンポーネント 子コンポーネント resources/assets/js/components/Product.vue <template> <div class="panel panel-default"> <div class="panel-body"> <div
class="row"> <div class="col-md-6">{{ product.name }}</div> <div class="col-md-6 text-right"> 価格 {{ product.price }}円 <button class="btn btn-default"><i class="fas fa-c </div> </div> </div> </div> </template> <script>
子から親へデータを渡す 子から親へデータを渡す 子から親へデータを渡す場合は、子で$emit を使いカ スタムイベントを発火させ、 親のテンプレート側の子コンポーネントのタグでv- on を使ってカスタムイベントをリッスンしデータを 渡す。
子コンポーネントであるProduct から、 親コンポーネントであるProductList に選択した商品 数とその値段を渡す場合
子コンポーネント 子コンポーネント resources/assets/js/components/Product.vue <template> <div class="panel panel-default"> <div class="panel-body"> <div
class="row"> <div class="col-md-6">{{ product.name }}</div> <div class="col-md-6 text-right"> 価格 {{ product.price }}円 <button class="btn btn-default" @click="add"><i c </div> </div> </div> </div> </template> <script>
親コンポーネント 親コンポーネント resources/assets/js/components/ProductList.vue <template> <div id="product-list" class="row"> <div class="col-md-8 col-md-offset-2">
<product v-for="product in productList" :product="product <div class="text-right">合計点数 {{ total.count }}点 合計 { </div> </div> </template> <script> import Product from './Product.vue' export default { components: { Product, }
親子じゃないコンポーネントにデータ 親子じゃないコンポーネントにデータ を渡す を渡す event bus という概念を使って、 グローバル領域にあるオブジェクトのインスタンスで イベントの発火を行い それをそれぞれのコンポーネントがリッスンすること
でデータをやりとりする。
コンポーネントProduct から コンポーネントCartLabel へ選択した商品数とその値 段を渡す場合
app.js app.js require('./bootstrap'); window.Vue = require('vue'); import ProductList from './components/ProductList.vue'
import CartLabel from './components/CartLabel.vue' const app = new Vue({ el: '#app', components: { ProductList, CartLabel, }, });
コンポーネントProduct コンポーネントProduct resources/assets/js/components/Product.vue <template> <div class="panel panel-default"> <div class="panel-body"> <div
class="row"> <div class="col-md-6">{{ product.name }}</div> <div class="col-md-6 text-right"> 価格 {{ product.price }}円 <button class="btn btn-default" @click="add"><i c </div> </div> </div> </div> </template> <script>
コンポーネントCartLabel コンポーネントCartLabel resources/assets/js/components/CartLabel.vue <template> <a class="btn btn-default" href="#"> <i class="fas
fa-shopping-cart"></i> <span class="badge b </a> </template> <script> export default { data() { return { count: 0, price: 0, } }, created() {
コンポーネントProductList コンポーネントProductList resources/assets/js/components/ProductList.vue <template> <div id="product-list" class="row"> <div class="col-md-8 col-md-offset-2">
<product v-for="product in productList" :product="product <div class="text-right">合計点数 {{ total.count }}点 合計 { </div> </div> </template> <script> import Product from './Product.vue' export default { components: { Product, }
▼それぞれのデータ受け渡しを理解するのに参考にな るページ https://kuroeveryday.blogspot.jp/2016/10/vuejs- components-emit-events.html
この程度のコンポーネント構造とイベント数であれ ば、そこまで複雑ではないし 「ボタン押したら2 つのコンポーネントにデータを渡 すのね」と覚えていられます。 が が このコンポーネント数が20 個になって、イベントが 15
個になったときのことを想像してみてください。
None
こうなります。
Vuex って何? Vuex って何?
戻ってきました。 いわゆるFlux フレームワークの1つで、 Vue.js で作成するアプリケーションの状態を管理する ためのフレームワーク。
Flux ? Flux ?
Facebook 社が提唱した設計思想。 矢印が一方向になることによって、デバッグがしやす くなりメンテナンス性が上がるという思想。
え? 逆に複雑そうじゃね?(小声)
あの図は偉い人用だから普通の人用に書き換えます
Action が呼ばれる ↓ Store の値が変わる ↓ View が書き換わる
Vuex 以外にもFlux フレームワークはた Vuex 以外にもFlux フレームワークはた くさんある くさんある Redux, Flux-utils,
MobX
では、なぜVuex なのか? では、なぜVuex なのか?
Vue.js のリアクティブシステムを利用することによ りView の効率的な更新を可能にしている 手順がRedux より少なく、単純で分かりやすい Chrome の開発ツールでVuex の状態変化をステッ プごとに確認できる
具体的に何がうれしいの? 具体的に何がうれしいの?
コンポーネントが共有しているdata を抽出し、それ をグローバルな唯一無二のデータとして管理でき る!!
さっきの例だと、 ProductList.vue にもCartLabel.vue にも count とprice があり、 これをイベントで両方更新しに行っている。 どちらかに何かがあってバグっていると、 本来は同一でなければいけないデータに差が。。
要は、関係してるコンポーネ 要は、関係してるコンポーネ ントを覚えてないといけなく ントを覚えてないといけなく てバグりやすい てバグりやすい
グローバルな領域にあるcount とprice をそれぞれのコ ンポーネントが読みに行ったり、 変更したりすればいいじゃないの! そう思いませんか? そう思いませんか?
そう、つまりそれがVuex
Vuex を使うとこうなるよ Vuex を使うとこうなるよ
それぞれのコンポーネントでは共通で使うデー それぞれのコンポーネントでは共通で使うデー タを読み込んで、それを更新するミューテーシ タを読み込んで、それを更新するミューテーシ ョンを呼ぶだけ ョンを呼ぶだけ
共通で使うデータやそれを更新するメソッドは new Vuex.Store でインスタンス化しておく const store = new Vuex.Store({ state:
{ total: { count: 0, price: 0, }, productList: [ { name: ' ', price: 140, onSale: true, }, { name: ' ', price: 300, onSale: true
app.js app.js require('./bootstrap'); window.Vue = require('vue'); import Vuex from 'vuex'
Vue.use(Vuex) import store from './store/vuex' import VxProductList from './components/VxProductList.vue' import VxCartLabel from './components/VxCartLabel.vue' const vxApp = new Vue({ el: '#app', store
コンポーネントVxProduct コンポーネントVxProduct resources/assets/js/components/VxProduct.vue this.$store はVuex ストアのインスタンス <template> <div class="panel panel-default">
<div class="panel-body"> <div class="row"> <div class="col-md-6">{{ product.name }}</div> <div class="col-md-6 text-right"> 価格 {{ product.price }}円 <button class="btn btn-default" @click="add"><i c </div> </div> </div> </div> </template> <script>
コンポーネントVxCartLabel コンポーネントVxCartLabel resources/assets/js/components/VxCartLabel.vue this.$store はVuex ストアのインスタンス <template> <a class="btn btn-default"
href="#"> <i class="fas fa-shopping-cart"></i> <span class="badge b </a> </template> <script> import { mapState } from 'vuex' export default { computed: { total() { return this.$store.state.total; }, }, }
コンポーネントVxProductList コンポーネントVxProductList resources/assets/js/components/VxProductList.vue this.$store はVuex ストアのインスタンス <template> <div id="product-list" class="row">
<div class="col-md-8 col-md-offset-2"> <vx-product v-for="product in productList" :product="produ <div class="text-right">合計点数 {{ total.count }}点 合計 { </div> </div> </template> <script> import { mapState, mapGetters } from 'vuex' import VxProduct from './VxProduct.vue' export default { components: { VxProduct
今回は触れなかったけど、mapState とか mapMutations という便利なメソッドを使うと、さら に簡潔にコードが書けます。
Laravel での導入手順 Laravel での導入手順
vuex をインストール vuex をインストール npm install vuex --save
app.js に以下の2 行を追加 app.js に以下の2 行を追加 import Vuex from 'vuex'
Vue.use(Vuex)
store を作成する store を作成する ※ 事前にVue とVuex を読み込んでおく(require or import)
必要があります。 const store = new Vuex.Store({ state: { count: 1 }, mutations: { increment (state) { // 状態 変更 state.count++ } } })
Vue のルートインスタンスにstore オプ Vue のルートインスタンスにstore オプ ションを追加 ションを追加 ※ ここで
store, となっているのは、ECMAScript 2015 で 変数名とオブジェクトのkey 名が同一の場合は省略できるという文法が追加になったためであり、 省略しない表記は store: store, です。 ※ store オプションのvalue にはVuex ストアのインスタンスを渡す必要があるので、ストアのインスタンス 生成はVue のルートインスタンスより前に行っておく必要があります。 const app = new Vue({ el: '#app', store, components: { VxProductList, VxCartLabel, }, });
要するに、簡単! 要するに、簡単!
つまり状態管理って何? つまり状態管理って何?
状態 = state = data 状態 = state = data
data をなぜstate と言い換えた? data をなぜstate と言い換えた?
data のとある状態を参照してるよ!ってこと
管理 = management = 整理してコントロール
今までdata の状態はいろんなコンポーネントに内包 されていて、それを変更するメソッドも分散しカオス っていました。
それを共通のストアで管理し、 一方向の流れでデータを変更していくことで整理整頓 された美しい世界を作る。
つまり、それが... That's 状態管理 That's 状態管理
【個人的な感想】 【個人的な感想】 js だろうが何だろうが、やっぱMVC が分かりやすいん じゃないそうなんじゃない? それに近づけたのがFlux なんじゃない? 違うの?そうでしょ?
その他 その他
▼公式ドキュメント(日本語訳版) https://vuex.vuejs.org/ja/
▼私が書いたLaravel+Vuex+Vue のサンプルコード ※ Vx のpre x ついてるコンポーネントのファイルがvuex 版 ※ assets/js/store
にjs をModule 化して分けて入れたり、mutations などのプロパティ別でファイルを分け て整理するとかするといいかもしれない。 https://github.com/fromarm4/vuex_study
▼Vuex を利用したVue アプリケーションのサンプル コードたち https://github.com/vuejs/vuex/tree/dev/examples
ご清聴ありがとうございまし ご清聴ありがとうございまし た! た!