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を学ぶ

第4章. コンポーネント間の相互作用

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

章では、ライフサイクル・フック、計算プロパティ、ウォッチャー、メソッド、その他の機能を使ってコンポーネントを構成する方法について深く掘り下げた。また、スロットの威力と、propsを使って他のコンポーネントから外部データを受け取る方法についても学んだ。

この章では、その基礎に基づき、カスタムイベントとprovide/injectパターンを使ってコンポーネント間のインタラクションを構築する方法を説明する。また、Teleport APIも紹介する。Teleport APIを使うと、Vueコンポーネント内で出現順序を保ったまま要素をDOMツリー内で移動させることができる。

Vueの入れ子コンポーネントとデータフロー

Vueコンポーネントは、その内部に他のVueコンポーネントをネストすることができる。この機能は、複雑なUIプロジェクトにおいて、ユーザがコードをより小さく、管理しやすく、再利用可能な部分に組織化するのに便利である。ネストされた要素を子コンポーネント、それを含むコンポーネントを親コンポーネント と呼び出す。

つまり、親コンポーネントは子コンポーネントにデータを渡すことができるが、その逆はできない。親コンポーネントはprops を使って子コンポーネントにデータを渡すことができ(「Options API の探索」で簡単に説明、子コンポーネントはカスタム・イベントemits を使って親コンポーネントにイベントを返すことができる。図4-1はコンポーネント間のデータの流れを示している 。

A diagram shows the one-way data flow between components
図4-1. Vueコンポーネントにおける一方向のデータフロー

関数をプロップとして渡す

他のフレームワークとは異なり、Vueでは関数を子コンポーネントにpropとして渡すことはできない。その代わりに、関数をカスタム・イベント・エミッターとしてバインドすることができる(カスタム・イベントを使ったコンポーネント間の通信」を参照 .

プロップを使って子コンポーネントにデータを渡す

オブジェクトまたは配列の形で、Vueコンポーネントのprops フィールドには、コンポーネントが親から受け取ることができるすべての利用可能なデータプロパティが含まれる。props の各プロパティは、ターゲット・コンポーネントのpropである。親からデータの受信を開始するには、例 4-1 に示すように、コンポーネントのオプション・オブジェクトでprops フィールドを宣言する必要がある。

例4-1. コンポーネントで小道具を定義する
export default {
  name: 'ChildComponent',
  props: {
    name: String
  }
}

例4-1ではChildComponent コンポーネントは、String 型のname プロップを受け取る。親コンポーネントは、例4-2 に示すように、このname プロップを使用して子コンポーネントにデータを渡すことができる。

例 4-2. 静的データを子コンポーネントにpropsとして渡す
<template>
  <ChildComponent ...
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