Vue 有很多内置指令,比如v-for、v-if,我们可以自定义类似的指令。
v-for
v-if
一个简单的自定义指令v-bg-color
v-bg-color
<template> <h1 v-bg-color="'skyblue'">This background is blue</h1> </template>
import {createApp} from 'vue' const app = createApp({}) app.directive('bg-color', { mounted(el, {value}) { // Update the background color el.style.background = value } })