Manon.icu

I'm here to make you a better developer by teaching you everything I know about building for the web.

Published 2022-03-31

自定义指令

Vue 有很多内置指令,比如v-forv-if,我们可以自定义类似的指令。

一个简单的自定义指令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
  }
})