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

第3章 コンポーネントの構成 コンポーネントを構成する

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

前の章では、Vueの基礎と、Options APIを使って一般的なディレクティブを持つVueコンポーネントを書く方法を学んだ。次のレベル、つまりリアクティビティやフックを使ってより複雑なVueコンポーネントを構成する方法について、深く掘り下げる準備が整った。

この章では、Vue Single File Component(SFC)標準、コンポーネントのライフサイクルフック、およびコンピューテッドプロパティ、ウォッチャー、メソッド、参照などの高度なリアクティブ機能を紹介する。また、スロットを使用して、スタイルでコンポーネントの構造を維持しながら、コンポーネントのさまざまな部分を動的にレンダリングする方法も学ぶ。この章が終わる頃には、アプリケーションで複雑なVueコンポーネントを書けるようになるだろう。

Vueシングルファイルコンポーネントの構造

Vueでは、新しいファイル形式規格であるVue SFCが導入され、.vue という拡張子で表記される。SFCを使用すると、コンポーネントのHTMLテンプレートコード、JavaScriptロジック、およびCSSスタイリングを同じファイルに、それぞれ専用のコードセクションに記述できる。Vue SFCには、3つの重要なコードセクション が含まれる:

テンプレート

このHTMLコードブロックは、コンポーネントのUIビューをレンダリングする。コンポーネントごとに、最上位の要素 に一度だけ表示される。

スクリプト

このJavaScriptコードブロックにはコンポーネントのメインロジックが含まれておりコンポーネントファイル 。

スタイル

このCSSコードブロックは、コンポーネントのスタイリングを含む。これはオプションであり、コンポーネントファイル ごとに必要なだけ出現させることができる。

例 3-1は、MyFirstComponent という名前の Vue コンポーネントの SFC ファイル構造の例である。

例3-1. MyFirstComponent コンポーネントのSFC構造
<template>
 <h2 class="heading">I am a a Vue component</h2>
</template>
<script lang="ts">
export default {
 name: 'MyFistComponent',
};
</script>
<style>
.heading {
  font-size: 16px;
}
</style>

図 3-1 に示すように、非 SFC コンポーネントコードを SFC にリファクタリングすることもできる。

Example of a Vue component created with single file component concept
図3-1. コンポーネントを非SFC形式からSFC形式にリファクタリングする。

図3-1が示すように、以下のリファクタリングを行う:

  • template フィールドの文字列値として表示されるHTMLコードを、Single File Componentの<template> セクションに移動する。

  • MyFirstComponent の残りのロジックを、シングルファイルコンポーネントの ...

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