vue 自定义指令
指令 和mounted 是什么关系 ?
**创建 工程:
H:\java_work\java_springboot\vue_study
ctrl按住不放 右键 悬着 powershell
H:\java_work\java_springboot\js_study\Vue2_3入门到实战-配套资料\01-随堂代码素材\day05\准备代码\04-自定义指令-基础语法
vue --version
vue create v-define-demo
cd v-define-demo
npm run serve
App.vue
<template>
<div>
<h1>自定义指令</h1>
<input v-focusg ref="inp" type="text" />
<input v-focus ref="inp" type="text" />
</div>
</template>
<script>
export default {
// 2.局部注册指令
directives: {
focus: {
inserted(el) {
el.focus();
},
},
},
};
</script>
<style>
</style>
main.js
import Vue from 'vue'
import App from './App.vue'
Vue.config.productionTip = false
Vue.directive('focusg', {
inserted(el) {
console.log(el);
el.focus()
}
})
new Vue({
render: h => h(App),
}).$mount('#app')