import{ createApp }from'vue'const app =createApp({})// 注册
app.directive('my-directive',{// Directive has a set of lifecycle hooks:// called before bound element's parent component is mountedbeforeMount(){},// called when bound element's parent component is mountedmounted(){},// called before the containing component's VNode is updatedbeforeUpdate(){},// called after the containing component's VNode and the VNodes of its children // have updatedupdated(){},// called before the bound element's parent component is unmountedbeforeUnmount(){},// called when the bound element's parent component is unmountedunmounted(){}})
import{ createApp }from'vue'import App from'./App.vue'const app =createApp(App)
app.directive('focus',{// When the bound element is mounted into the DOM...mounted(el){// Focus the element
console.log(el);
el.focus()}})
app.mount('#app')
1. 指令生命周期关键字变更在3.0中指令的注册和其生命周期是这样的import { createApp } from 'vue'const app = createApp({})// 注册app.directive('my-directive', { // Directive has a set of lifecycle hooks: // called before bound element's parent component is mounted beforeMount