vue 指令实例
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>自定义指令</title>
<!-- 引入样式 -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<style>
</style>
</head>
<body>
<div id='app'>
<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:#c23531;"></span>
<h1 v-hello>{{msg}}</h1>
<button @click='change'>改变msg内容</button>
<button @click="handleClick">clickMe</button>
<h1 v-if="count < 6" v-change="count">it is a custom directive</h1>
<el-row>
<el-col :span="24" v-for="(item, index) in 2" :key="item">
<div><label>自定义文件</label></div>
<el-card :body-style="{ padding: '0px' }" v-for="(o, index) in 5" :key="o" style="width:200px;display:inline-block;float:left;margin:20px;">
<img src="http://h.hiphotos.baidu.com/image/pic/item/730e0cf3d7ca7bcba51475b9b2096b63f624a83d.jpg" class="image" width="200" height="200">
<div style="padding: 14px;">
<span>好吃的汉堡</span>
<div class="bottom clearfix">
<time class="time">{{ currentDate }}</time>
<el-button type="text" class="button">操作按钮</el-button>
</div>
</div>
</el-card>
<el-card :body-style="{ padding: '0px' }" v-if="item == 2" style="width:200px;display:inline-block;float:left;margin:20px;">
<img src="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=655586665,2158258541&fm=27&gp=0.jpg" class="image" width="200" height="200">
<!-- <div style="padding: 14px;">
<span>好吃的汉堡</span>
<div class="bottom clearfix">
<time class="time">{{ currentDate }}</time>
<el-button type="text" class="button">操作按钮</el-button>
</div>
</div> -->
</el-card>
</el-col>
</el-row>
</div>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- 引入组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
Vue.directive('hello',{
bind(el , binging){ // 指令第一次绑定到元素的时候执行,且只执行一次,一般用于执行初始化操作
console.log('bind',el , binging)
},
inserted(el , binging){ // 当被绑定的元素插入到DOM中时执行
console.log('inserted',el , binging)
},
update(el , binging){ // 当被绑定的元素所在模版更新时执行
console.log('update',el , binging)
},
componentUpdated(el , binging){ // 一般不怎么用,表示被绑定元素所在模板完成一次更新周期时调用,个人感觉效果与update有点重合
console.log('componentUpdated',el , binging)
},
unbind(el , binging){ // 指令与被绑定的元素解绑时使用,只调用一次
console.log('unbind',el , binging)
}
})
var vm = new Vue({
el:'#app',
data:{
msg:'hello world',
currentDate: new Date(),
count:0
},
methods:{
change(){
this.msg = this.msg.split('').reverse().join('')
},
handleClick: function () {
//按钮单击,count自增
this.count++;
}
},
directives:{
change:{
bind: function (el,bindings) {
console.log('指令已经绑定到元素了');
console.log(el);
console.log(bindings);
//准备将传递来的参数
// 显示在调用该指令的元素的innerHTML
el.innerHTML = bindings.value;
},
update: function (el,bindings) {
console.log('指令的数据有所变化');
console.log(el);
console.log(bindings);
el.innerHTML = bindings.value;
if(bindings.value == 5)
{
console.log(' it is a test');
}
},
unbind: function () {
console.log('解除绑定了');
}
}
}
})
</script>
</body>
</html>
阅读更多- Vue.js学习记录之在元素与template中使用v-if指令实例
- Vue框架-基础知识(vue指令、实例生命周期、计算属性与监听属性、插槽理解、组件介绍、数据交互、ES6语法等)
- vue.js指令和组件详细介绍及实例
- Vue指令练习实例-学生信息录入
- Vue.js基础指令实例讲解(各种数据绑定、表单渲染大总结)
- Vue的土著指令和自定义指令实例详解
- VUE学习笔记(Vue指令及实例)
- Vue------第一天(Vue实例;模板语法:插值、指令、缩写)
- vue的指令实例属性
- Vue实例的属性和方法
- vue快速入门的三个小实例
- 基于vue+canvas的excel-like组件实例详解
- Vue2.0 http请求以及loading展示实例
- vue——实例方法 / 数据
- vue+ElementUI实现订单页动态添加产品数据效果实例代码
- vue实例
- vue-cli入门(三)——人员管理实例
- Vue.js+Layer表格数据绑定与实现更新的实例
- Vue学习笔记——指令和部分组件
- 《AngularJS》5个实例详解Directive(指令)机制