微信小程序项目遇到的写法问题

442 阅读2分钟

这是我参与11月更文挑战的第13天,活动详情查看:2021最后一次更文挑战

微信小程序使用van-weapp组件库时,微信小程序控制台警告:

[Component] slot "" is not found.

经过多次测试后发现是微信小程序基础库版本问题,只要是微信小程序的基础库不高于2.18.0就不会出现这个警告。

微信小程序 this.setData方法修改data中对象的某个属性:

this.setData({['config.scaleId']: 5,})

把config对象中的属性用字符串包裹起来放到数组中

微信小程序动态绑定类class的写法:

<view class="[Student,{{select=='student' ? 'select':''}}]" bindtap="ChooseIdentity"></view>

微信小程序使用vant组件

vant的组件,如果原组件设置了高度,自己想去掉高度,可以用height:auto !important; 覆盖原样式

微信小程序 getstorageSync 和getStorage

在js中发送请求时需要向后台发送缓存中的数据时,可能会获取不到,原因是获取缓存getStorage是异步的,wx.request也是异步的,有时可能缓存还没获取到就已经发送了请求,此时应该用getStorageSync方法同步获取缓存,等待缓存获取完成后再发送请求

微信小程序使用van-image组件,src属性如何动态绑定:

<van-image width="40rpx" height="30rpx" src="{{select=='true'?'/images/seat_my.png':'/images/seat_nocan.png'}}" />

微信小程序wx:for中的index和item怎么应用到class和style属性中:

<view class="nb" wx:for="{{num}}">
        <view  style="{{(index+1)%3==0?'margin-right:50rpx'></view>
</view>

微信小程序setData方法修改数组中某个索引的值,并且用变量当做索引:

let dataID = self.data.dataID;
self.setData({[`tabledata[${dataID}]`]:res.data.data.data})

微信小程序,绑定事件传递参数(自定义属性) data- 后面的名字不支持大写,如果大写,微信小程序还是会解析成小写

 <text decode="true" bindtap="gotoLocation" data-ordertime="{{projectDetail.classDate}}" >{{location=='null'?'':location}}</text>

比如以上代码中的data-ordertime如果写成data-orderTime,在onLoad函数的options参数中,如果要拿到这个属性,还是得写成options.ordertime

微信小程序格式化时间函数:

const formatTime = time => 
{
  const date = new Date(time);
  console.log(date);
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const date = new Date(time);为了统一传入的参数,只有 Fri Nov 26 2021 15:25:39 GMT+0800 (中国标准时间) 这种格式的时间才能使用date.getFullYear()等方法