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

第7章. 高度なレンダリング、ダイナミック・コンポーネント、プラグイン合成

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

前の章では、Vueの仕組み、Options APIとComposition APIを使ったコンポーネントの合成方法、Axiosを使って外部リソースのデータをVueアプリケーションに取り込む方法について学んだ。

この章では、Vueにおけるレンダリングのより高度な側面を紹介する。レンダリング関数とJSXを使って関数型コンポーネントを計算する方法と、Vueのコンポーネントタグを使って要素を動的かつ条件付きでレンダリングする方法を探る。また、アプリケーション内で使用するカスタムプラグインの登録方法についても学ぶ。

レンダー関数とJSX

VueコンパイラAPIにより、Vueはレンダリング時に、Vueコンポーネントに使用されるすべてのHTMLテンプレートを処理し、Virtual DOMにコンパイルする。Vueコンポーネントのデータが更新されると、Vueは内部のレンダー関数をトリガーして、最新の値をVirtual DOM に送信する。

template 、コンポーネントを作成する最も一般的な方法である。しかし、パフォーマンスの最適化、サーバ側レンダリング・アプリケーションでの作業、ダイナミック・コンポーネント・ライブラリでの作業など、特定のシナリオではHTMLテンプレート・パーサーのプロセスをバイパスする必要がある。仮想DOMからレンダリングされた仮想ノードを直接返し、テンプレートのコンパイル処理をスキップすることで、render() 、このような場合の解決策となる。

レンダー関数を使う

Vue 2では、render() 関数のプロパティは、createElement コールバックパラメータを受け取る。この関数は、適切な引数で をトリガすることによって、有効な VNode1createElement を適切な引数でトリガーする。通常、createElementh 関数と呼ぶ。2

例7-1は、Vue 2の構文でコンポーネントを作成する例を示している。

例7-1. Vue 2でrender関数を使用する
const App = {
 render(h) {
  return h(
   'div',
   { id: 'test-id' },
   'This is a render function test with Vue'
  )
 }
}

このコードは、以下のテンプレート・コード を書くことに等しい:

const App = {
 template: `<div id='test-id'>This is a render function test with Vue</div>`
}

Vue 3では、render の構文が大きく変わった。h 関数をパラメータとして受け付けなくなった。その代わりに、vue パッケージは、VNode を作成するためのグローバル関数h を公開している。したがって、例7-1のコードを例7-2のように書き換えることができる。

例7-2. Vue 3でrender関数を使用する
import { createApp, h } from 'vue'

const App = {
 render() {
  return h(
   'div',
   { id: 'test-id' },
   'This is a render function test with Vue' ...
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