当前位置:   article > 正文

原生微信小程序_微信小程序原生点击事件

微信小程序原生点击事件

1、点击事件:

bindtap:在普通冒泡事件的捕获阶段触发的

catchtap:阻止事件冒泡

2、数据绑定:

使用插值表达式

  1. <view>{{info.title}}--{{info.desc}}</view>
  2. <image src="{{imgSrc}}" alt="" />
  3. <view>{{num}}----{{num>10?'数字大于10':'数字小于等于10'}}</view>

3、更改数据:

使用setData来更改数据

  1. this.setData({
  2. clickNum:this.data.clickNum+100
  3. })

4、bindinput事件:

用于响应input框的输入事件,相当于为文本框绑定input事件,input框中的value动态绑定值

<input type="text" value="{{inputValue}}" bindinput="inputHandler" class="border-btn" />
  1. inputHandler(e) {
  2. console.log(e.detail.value);
  3. }

5、控制元素显示与隐藏:

wx:if:动态创建和移除元素,控制元素显示与隐藏(控制条件复杂时建议使用wx:if搭配wx:elif和wx:else)

hidden:以切换样式的方式(display:none/block),控制元素的显示隐藏(频繁切换时建议使用hidden)

  1. <button bindtap="change">点击改变状态
  2. <view wx:if="{{show}}">显示</view>
  3. <view wx:else>隐藏</view>
  4. </button>
  5. <view hidden="{{!show}}">条件为true显示,条件为false隐藏</view>

6、列表循环渲染:

使用wx:for渲染列表,默认索引和当前项就是index和item,使用wx:key指定唯一的key值,提高渲染效率

  1. <view wx:for="{{list}}" wx:key="index">
  2. index:{{index}}--item项:{{item.title}}--{{item.desc}}
  3. </view>

索引和当前项可更改名字,使用wx:for-index和wx:for-item更名

  1. <view wx:for="{{list}}" wx:for-index="idx" wx:for-item="value" wx:key="idx">
  2. index:{{idx}}--item项:{{value.title}}--{{value.desc}}
  3. </view>

7、传参

使用data-参数名=“参数”的形式传参

  1. <button bindtap="click" data-num="12" data-title="我是标题">
  2. 点击+100--{{clickNum}}
  3. </button>
  1. click(e) {
  2. console.log(e.target.dataset.num);
  3. console.log(e.target.dataset.title);
  4. this.setData({
  5. clickNum: this.data.clickNum + 100
  6. })
  7. },

8、组件传值

父传子:

在父组件的json文件中引用子组件:

  1. {
  2. "usingComponents": {
  3. "test1":"/components/test1/index"
  4. }
  5. }
<test1 title="{{title}}"></test1>

在子组件的js文件中properties里面接收

  1. properties: {
  2. title:''
  3. },
<text>父组件传过来的title----{{title}}</text>

子传父:

在子组件的事件中使用triggerEvent自定义一个方法名传递数据

  1. addNum(){
  2. this.setData({
  3. count:this.properties.count+=1
  4. })
  5. this.triggerEvent('sync',{value:this.properties.count})
  6. }

在父组件中,使用bind:自定义方法名接收数据

 <test1 title="{{title}}" count="{{count}}" bind:sync="syncCount"></test1>
  1. syncCount(e){
  2. this.setData({
  3. console.log(e)
  4. count:e.detail.value
  5. })
  6. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/489710
推荐阅读
相关标签
  

闽ICP备14008679号