Skip to Content
Vueを学ぶ
book

Vueを学ぶ

by Maya Shavin
March 2025
Intermediate to advanced
350 pages
5h 20m
Japanese
O'Reilly Media, Inc.
Content preview from Vueを学ぶ

第9章. Piniaによる状態管理

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

前の章では、ネストされたルート、ルート・ガード、ダイナミック・ルート・ナビゲーションなど、Vue Routerを使ったアプリケーションのルーティングの構築について説明した。

この章では、Vueで公式に推奨されている状態管理ライブラリであるPiniaを使用して、状態管理とVueアプリケーション内のデータフローを管理する方法を学ぶ。また、アプリケーションの再利用可能で効率的なデータ状態管理システムを構築する方法を探る。

Vueの状態管理を理解する

データはアプリケーションに生命を吹き込み、コンポーネントをつなぐ。そしてコンポーネントは、データの状態を使ってユーザや他者と対話する。状態管理は、サイズや複雑さに関係なく、実際のデータで動作するアプリケーションを構築する上で非常に重要だ。例えば、ピザとその詳細のリストを持つ商品カードのギャラリーだけを表示することができる。ユーザがこのギャラリーコンポーネント内のカートに商品を追加したら、カートのデータを更新し、選択した商品の残りの在庫を更新すると同時に、更新されたカートのデータをカートコンポーネントに表示する必要がある 。

例えば、ピザ・ハウスのアプリケーションを見てみよう。メインビュー(App.vue)には、ヘッダコンポーネント(HeaderView)とピザカードのギャラリー(PizzasView)がある。ヘッダにはカートアイコンがあり、カートにあるアイテムの数を表示する。一方、ギャラリーにはピザカードのリストがあり、それぞれユーザが選択したアイテムをカートに追加できるボタンがついている。図9-1はメインビューからのコンポーネントの階層構造を示している。

Screenshot showing the tree structure of the App component, where the header component and the gallery component are the children of the App and sibling to each other
図9-1. Pizza Houseのメインビューから見たコンポーネントの階層構造

ユーザがカートにピザを追加すると、カートアイコンは更新されたアイテム数を表示する。ヘッダコンポーネントとギャラリーコンポーネントの間のデータ通信を可能にするために、図9-2で見られるように、Appcart データを管理させ、イベントupdateCart を使ってギャラリーと通信しながら、そのデータをpropsとしてヘッダに渡すことができる。

Screenshot showing the data flow between the components, where the App component manages the cart data and passes it to the header component as props, and communicates with the gallery component using an event updateCart
図9-2. ギャラリーとヘッダ間のデータフロー(アプリを仲介とする

このアプローチは、小規模なアプリケーションではうまくいく。しかし、PizzasView を、PizzasGallery のようなサブコンポーネントに分割し、PizzasGallery に各ピザ用のPizzaCard コンポーネントをレンダリングさせたいとする。それぞれの新しい親子レイヤーに対して、図9-3のように、ギャラリーとヘッダ間のデータフローの伝搬を確実にするために、updateCart イベントを伝搬する必要がある。

コンポーネントやレイヤーが増えると、より複雑になり、不必要なプロップやイベントがたくさん残ることになる。その結果、このアプローチは、アプリケーションが成長したときにスケーラビリティとメンテナンス性が低下する可能性がある。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

エンジニアが学ぶ会計システムの「知識」と「技術」

エンジニアが学ぶ会計システムの「知識」と「技術」

広川 敬祐, 五島 伸二, 小田 恭彦, 大塚 晃, 川勝 健司
グリーンなソフトウェア開発

グリーンなソフトウェア開発

Anne Currie, Sarah Hsu, Sara Bergman
強力なPython

強力なPython

Aaron Maxwell

Publisher Resources

ISBN: 9798341626102