ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
| この記事の要点 |
|---|
|
なぜ nextTick が必要か
Vue は data 変更を検知すると次のティックで DOM を更新します(バッチ処理によるパフォーマンス最適化)。data を変更した直後に DOM を参照しても、まだ更新されていません。
// 例: nextTick なしだと
data() {
return { message: "" };
},
methods: {
update() {
this.message = "Hello";
// この時点で this.$el.textContent は "" のまま!(DOM はまだ更新されていない)
const len = this.$el.querySelector("p").textContent.length;
console.log(len); // → 0
}
}
nextTick で DOM 反映を待つ
// Vue 2 / Vue 3 Options API
methods: {
update() {
this.message = "Hello";
this.$nextTick(() => {
// DOM 反映完了後
const len = this.$el.querySelector("p").textContent.length;
console.log(len); // → 5
});
}
}
// async / await でも書ける
methods: {
async update() {
this.message = "Hello";
await this.$nextTick();
const len = this.$el.querySelector("p").textContent.length;
console.log(len);
}
}
Vue 3 Composition API
<script setup>
import { ref, nextTick } from "vue";
const message = ref("");
const el = ref(null);
async function update() {
message.value = "Hello";
await nextTick();
// DOM 反映完了
console.log(el.value.textContent); // → "Hello"
}
</script>
<template>
<p ref="el">{{ message }}</p>
<button @click="update">更新</button>
</template>
典型的な使用シーン
① 動的に追加した要素のサイズ計算
async addItem(item) {
this.items.push(item); // リストに追加
await this.$nextTick();
// 新しい要素の高さを計算
const newElem = this.$refs.list.lastElementChild;
const height = newElem.getBoundingClientRect().height;
console.log("追加した要素の高さ:", height);
}
② スクロール位置の調整
async scrollToBottom() {
this.messages.push({ text: "新メッセージ" });
await this.$nextTick();
// メッセージが描画されてからスクロール
const container = this.$refs.messageContainer;
container.scrollTop = container.scrollHeight;
}
③ フォーカス制御
async toggleEditMode() {
this.editing = true; // v-if で input 表示
await this.$nextTick();
// input が描画されてからフォーカス
this.$refs.editInput.focus();
}
④ サードパーティライブラリの再初期化
async updateChart() {
this.chartData = newData;
await this.$nextTick();
// Chart.js を再描画
this.chart.update();
}
async refreshSlider() {
this.slides = newSlides;
await this.$nextTick();
// Swiper を再初期化
this.swiper.update();
}
watch との組み合わせ
// Vue 3 Composition API
import { watch, nextTick, ref } from "vue";
const items = ref([]);
watch(items, async (newItems) => {
await nextTick();
// items が変更されて DOM が更新された後
console.log("DOM 更新完了");
});
nextTick が複数回ある場合
同じティック内の複数の data 変更は1 回の DOM 更新にまとめられます:
methods: {
async updateMany() {
this.a = 1;
this.b = 2;
this.c = 3;
// すべての変更が 1 回の DOM 更新で反映される
await this.$nextTick();
// 全部反映済み
}
}
nextTick の代替
シンプルなケースなら setTimeout(fn, 0) や requestAnimationFrame でも近い動作になりますが、Vue の更新サイクルと完全に同期する保証はないため nextTick を使うのが安全:
// ✗ 動くこともあるが、Vue の更新と同期しないことも
this.message = "Hello";
setTimeout(() => {
console.log(this.$el.textContent);
}, 0);
// ✓ 確実
this.message = "Hello";
this.$nextTick(() => {
console.log(this.$el.textContent);
});
注意点
無限ループに注意
// ✗ 危険
async update() {
this.count++;
await this.$nextTick();
this.count++; // また data 変更 → さらに DOM 更新サイクル
// → updated フックや watcher で意図せず連鎖発火することがある
}
テストでも nextTick
// Vue Test Utils
import { mount } from "@vue/test-utils";
import { nextTick } from "vue";
test("counter increments", async () => {
const wrapper = mount(Counter);
await wrapper.find("button").trigger("click");
await nextTick(); // DOM 反映を待つ
expect(wrapper.text()).toContain("Count: 1");
});
関連記事
ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
子ページ
子ページはありません
同階層のページ
- インストール(ファイルのダウンロード)
- npmを使用したプロジェクトの作成
- for 繰り返し処理
- ifの条件分岐とtemplateを用いたグループ化
- クリック時のイベント処理(on:click)
- modelとdata フォーム入力値とDOMへの即時反映
- computed(算出プロパティ)と使い方とdataとの違い
- ライフサイクルフック(created / mounted / updated / destroyedの使い方)
- $nextTickの使い方(ライフサイクルフック)
- メソッドの定義方法
- エラー一覧
- ルーティング設定
- aリンクの貼り方と動的URLの作成
- Mixinを利用した共通処理の記述方法
- v-bindによるデータ連携
- ヘッダー/フッターの共通コンポーネント
- ナビゲーションの現在ページをハイライトする方法
- 画面サイズの取得方法
人気ページ
- 1 Eclipseで「サーバーに追加または除去できるリソースがありません。」の原因と対処法
- 2 tomcat の起動 / 停止ログと catalina.log・catalina.out の違い
- 3 JavaScript で base URL を取得する方法|window.location.origin
- 4 YouTube Data API v3 エラー一覧|403・400・404 の原因と対処
- 5 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 6 3Dグラフィックスとは|モデリング/レンダリング/主要ソフトウェア (Blender / Maya)
- 7 Spring Frameworkのアノテーション一覧
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- 【django】ログイン 認証機能 2026-09-08 03:22:09
- MySQL ERROR 1063 Incorrect column specifier for column|原因と直し方 2026-09-08 03:11:09
- 【PHPエラー】Object of class stdClass could not be converted to string 2026-09-07 07:46:04
- Julia Genie ローカル開発サーバ起動完全ガイド 2026-09-07 07:46:04
- Wi-Fi とは|規格と世代(Wi-Fi 4〜7)・周波数帯・CSMA/CA・WPA3 NEW 2026-09-07 07:45:13
- set コマンドでシェルオプションと位置パラメータを操作 | bash 2026-09-07 07:45:13
- JPEG(.jpg/.jpeg)画像形式の完全ガイド — 仕様・マジックナンバー・他形式との比較・EXIF 2026-09-07 07:45:13
- UE5 Get Overlapping Actorsで特定クラスだけ処理|Class Filter・Cast To 2026-09-07 07:45:13
- Linuxで特定拡張子のファイルを再帰削除|find -deleteの安全手順 2026-09-07 07:45:13
- Django CBVでテンプレートに値を渡す|get_context_data・extra_context 2026-09-07 07:45:13
- Linuxで複数ファイルの文字列を一括置換|find・sed・xargsの安全手順 2026-09-07 07:45:13
- Laravelルートグループ|prefix・middleware・nameで一括管理 2026-09-07 07:45:13
- Linux whichコマンドの使い方|絶対パス取得とtype・command -vの違い 2026-09-07 07:45:13
- Linux catコマンドの使い方|行番号表示・複数ファイル連結 2026-09-07 07:45:13
- unable to execute 'gcc': No such file or directory 2026-09-07 07:45:13
コメントを削除してもよろしいでしょうか?