vue自定义指令

复制代码
app.vue
<template>
  <div class="">
    <!-- 自定义指令全局
    <input v-focus type="text" name="" id=""><br>
    自定义指令局部
    <input v-focus2 type="text" name="" id=""><br> -->
    <span v-color="corlor">修改指令的值1</span><br>
    <span v-color="corlor1">修改指令的值2</span>
  </div>
</template>

<script>
export default {
  data(){
    return {
      corlor:'red',
      corlor1:'blue'
    }
  },
  directives:{
    // focus2:{
    //   inserted(el){
    //     el.focus()
    //   }
    // },
    color:{
      //inserted只会在页面加载的时候将值渲染上去
      inserted(el,binding){
        el.style.color=binding.value     
      },
      //update会在值修改的时候渲染上去
      update(el,binding){
        el.style.color=binding.value 
      }
    }
  }

}
</script>

<style>

</style>
复制代码
复制代码
index.js
import Vue from 'vue'
import App from './App.vue'

Vue.config.productionTip = false

// //全局指令
// Vue.directive('focus',{
//   inserted(el){
//     //el就是绑定的元素
//     //实现一进入就获取聚焦
//     el.focus()
//   }

// })

new Vue({
  render: h => h(App),
}).$mount('#app')
复制代码

 

posted @   超爱彬宝同学  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示