摘要:
小程序开发过程中,编写一个组件是很常见的.但是有时候,我们对这个组件进行操作的时候,希望能通过绑定的事件,进行提交我们的数据到服务器上.那么这时候,就需要我们创建自定义事件!//&... 小程序开发过程中,编写一个组件是很常见的.但是有时候,我们对这个组件进行操作的时候,希望能通过绑定的事件,进行提交我们的数据到服务器上.那么这时候,就需要我们创建自定义事件!
// components/like/index.js
Component({
/**
* 组件的属性列表
*/
properties: {
like:{
type:Boolean//默认false
},
count:{
type:Number//默认0
}
},
/**
* 组件的初始数据
*/
data: {
// like:false,
// count:9,
yesSrc:'images/like.png',
noSrc:'images/like_no.png'
},
/**
* 组件的方法列表
*/
methods: {
onLike:function(event){
// console.log(event)
let like=this.properties.like
let count=this.properties.count
count = like?count-1:count+1
this.setData({
count:count,
like:!like
})
//设置事件激活事件
let behavior=this.properties.like?'like':'cancel'
this.triggerEvent('like',{
behavior:behavior
},{})
}
}
})
---------------
/**
* 组件的方法列表
*/
methods: {
onLeft:function(event){
//如果不是最后一期,则触发
if(!this.properties.latest){
this.triggerEvent('left', {}, {})
}
},
onRight: function (event) {
//如果不是第一期,则触发
if(!this.properties.first){
this.triggerEvent('right', {}, {})
}
}
}

