「ど 部分が変更されるか」というパッチフラグを付与し、実行時にこ フラグに
基づいて必要な更新 みを行い、不要な比較処理を完全にスキップする仕組み
パッチフラグ( Patch Flag)
Vue3で 改善: Compiler-Informed VDOM
これ 静的なテキストです。
{{ message }}
クラスが動的です。
IDとdisabledが
動的
import { ref } from 'vue'
export default {
setup() {
const message = ref('こんにち ');
const isActive = ref(true);
const dynamicId = ref('my-id');
const isDisabled = ref(false);
return { message, isActive, dynamicId, isDisabled };
}
}
import { openBlock, createElementBlock, createElementVNode,
toDisplayString, normalizeClass } from "vue"
// 1. 静的な要素 Static Hoistingされる (Patch Flag: -1)
const _hoisted_1 = /*#__PURE__*/createElementVNode("p", { class: "static" },
"これ 静的なテキストです。", -1 /* HOISTED */)
export function render(_ctx, _cache, $props, $setup, $data, $options) {
return (openBlock(), createElementBlock("div", null, [
_hoisted_1,
// 2. テキストだけが動的 -> Patch Flag: 1 (TEXT)
createElementVNode("p", null, toDisplayString(_ctx.message), 1 /* TEXT */),
// 3. classだけが動的 -> Patch Flag: 2 (CLASS)
createElementVNode("div", {
class: normalizeClass({ active: _ctx.isActive })
}, "クラスが動的です。", 2 /* CLASS */),
// 4. class/style以外 属性が動的 -> Patch Flag: 8 (PROPS)
// 第5引数で、動的な属性名を具体的に指定 ["id", "disabled"]
createElementVNode("button", {
id: _ctx.dynamicId,
disabled: _ctx.isDisabled
}, "IDとdisabledが動的", 8 /* PROPS */, ["id", "disabled"]),
// 5. 複数 動的バインディング -> Patch Flag ビット演算で結合される
// 16 (FULL_PROPS) が使われることが多い
createElementVNode("input", {
id: _ctx.dynamicId,
class: normalizeClass({ active: _ctx.isActive }),
value: _ctx.message
}, null, 16 /* FULL_PROPS */)
]))
}
コンポーネント ソースコード コンパイル後 コード