<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>指令</title>
<style>
[v-cloak]{
display: none;
}
</style>
<script src="../tool/vue.js"></script>
</head>
<body>
<div id="app">
<p ref="content">atguigt.com</p>
<button @click="hint">提示</button>
<p v-cloak>{{msg}}</p>
<div>
<p v-upper-text="msg2"></p>
<p v-lower-text="msg2"></p>
</div>
</div>
<script type="text/javascript" >
/*
* 定义全局指令
* el:指令属性所在的标签对象
* binding :包含指令相关数据的对象
* */
Vue.directive('upper-text',function (el,binding) {
console.log(el,binding);
el.textContent=binding.value.toUpperCase();
})
var vm=new Vue({
el:'#app',
data:{
msg:'血虚',
msg2:'I love you'
},
//内部指令
directives:{
'lower-text':function (el,binding) {
console.log(el,binding);
el.textContent=binding.value.toLowerCase();
}
},
methods:{
hint(){
alert(this.$refs.content.textContent);
}
}
})
</script>
</body>
</html>
效果图: