- 引用指令时,指令名前面加上 v-
- 直接在元素上在使用即可 : v-指令名='表达式'
代码展示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<p v-upper-text="message"></p>
自动获取焦点:<input type="text" name="" id="" v-focus>
</div>
<script src="./node_modules/vue/dist/vue.js"></script>
<script>
// 注册全局指令,注意指令名不要带 v-
Vue.directive('upper-text',{
bind: function(el){
el.style.color = 'red'
},
inserted: function(el,binding){
el.innerHTML = binding.value.toUpperCase();
}
})
new Vue({
el: '#app',
data: {
message: 'cyz,陪你学习,伴你成长!'
},
// 注册局部指令
directives:{
'focus': {
inserted: function(el,binding){
el.focus()
}
}
}
})
</script>
</body>
</html>
gitee源码地址:
https://gitee.com/cyzgw/vue_demo.git