小程序实践小坑小结(一)

最近自己在做小程序练习,分享一下我遇到的小坑

data数据更新

  • 直接对this.data进行赋值,是无法更新视图绑定的数据的,会造成数据不一致
  • 需要使用this.setData更新
this.data.key = value
this.setData({
  key: value
})

require

  • 暂时不支持绝对路径
const util = require('../../utils/util.js')

background-image

  • 不能使用静态文件,只能使用base64和网络图片
  • 可以用<img>解决
background: #fff url(data:image/jpeg;base64,***)
<image class="logo" src="/images/logo.png" mode="cover"></image>

组件样式

  • app.wxss 的样式不能应用到组件内部
  • 可以按需引用 import: “”
@import "/app.wxss";

textarea

  • textarea默认样式有固定宽度

事件传参

  • 模板里面事件不能传参
  • 使用event.currentTarget.dataset获取
<view id="tapTest" data-hi="WeChat" bindtap="tapName"> Click me! </view>

Page({
  tapName(event) {
    console.log(event.currentTarget.dataset.hi)
  }
})

animation

  • animation不能直接绑定中组件上
  • 外面包裹一层<view>
<view animation={{animation}}>
  <my-component></my-component>
</view>

checkBox

  • checkbox-group绑定的bindChange事件,我们中点击checkbox事件会向上冒泡,导致外层也被点击
  • checkBox外面包一层view,给view添加一个catch事件
<checkbox-group bindchange="checkboxChange">
  <view bindtap="bindTap">
    <view catchtap='catchTap'">
     <checkbox value="{{value}}" checked="{{checked}}"/>
    </view>
  </view>
</checkbox-group>

本文转载于:猿2048https://www.mk2048.com/blog/blog.php?id=h0ahic22b2j

猜你喜欢

转载自www.cnblogs.com/10manongit/p/12704546.html
今日推荐