本文作者:DurkBlue

如何使插件中事件的激活

DurkBlue 2020-01-11 1632 抢沙发
如何使插件中事件的激活摘要: 小程序开发过程中,编写一个组件是很常见的.但是有时候,我们对这个组件进行操作的时候,希望能通过绑定的事件,进行提交我们的数据到服务器上.那么这时候,就需要我们创建自定义事件!//&...

小程序开发过程中,编写一个组件是很常见的.但是有时候,我们对这个组件进行操作的时候,希望能通过绑定的事件,进行提交我们的数据到服务器上.那么这时候,就需要我们创建自定义事件!


// 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', {}, {})
      }
    }
 
  }


这篇文章由DurkBlue发布,转载请注明来处
文章投稿或转载声明

来源:DurkBlue版权归原作者所有,转载请保留出处。本站文章发布于 2020-01-11
温馨提示:文章内容系作者个人观点,不代表DurkBlue博客对其观点赞同或支持。

赞(0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

阅读
分享

发表评论取消回复

快捷回复:
AddoilApplauseBadlaughBombCoffeeFabulousFacepalmFecesFrownHeyhaInsidiousKeepFightingNoProbPigHeadShockedSinistersmileSlapSocialSweatTolaughWatermelonWittyWowYeahYellowdog

评论列表 (暂无评论,1632人围观)参与讨论

还没有评论,来说两句吧...