小程序 遇到问题

2022-03-21  本文已影响0人  autumn_3d55

1. 给对象的属性赋值

因为小程序不能直接给对象属性赋值,要通过特殊处理 ['value']

data:
fromData: {
  inputValue: '',
}

handleTextArea(e) {
    let inputValue = 'formData.inputValue'
    this.setData({
      [inputValue]: e.detail.value
    })
  },

2. 微信小程序在wxml双大括号中执行复杂运算

微信小程序数据绑定支持双大括号中的表达式,但目前只支持这四种表达式:算数表达式、关系表达式、字符串连接表达式、三元表达式。vue则是全功能的JS表达式。使用起来明显差别在于是否支持函数/方法表达式

1. 遇到的问题

<!-- 有效 -->
  <view>{{ 1+1 }}</view>
  <view>{{ 2>1 ? '是':'否'}}</view>

  <!-- 无效 -->
  <view>{{ Math.random() > 0.5 ? '大于':'小于' }}</view>
  <view wx:for="{{'1,2,3'.split(',')}}">
      {{item}}
  </view>

2.解决方法

<view wx:for="{{fn.split(str)}}">
        {{item}}
    </view>
    <wxs module="fn">
      module.exports = {
          split: function(str){
            return str.split(',');
          }
      }
    </wxs>

3.实例

<view class="imgList">
              <wxs module="fn">
                module.exports = {
                  split: function (str) {
                    return str.split(',');
                  }
                }
              </wxs>
              <block wx:for="{{fn.split(item.COMIMG)}}">
                <image src="{{imgAddr}}/{{item}}"></image>
              </block>
            </view>

3. 小程序tabbar跳转用到的 wx.switchTab 没法携带参数

跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面

可通过 app.js 添加全局属性

app.js

globalData: {
  currentIndex: 0
}

index.js

//通过调用全局属性更改数据
//订单跳转
  toOrderListTap(e) {
    getApp().globalData.currentIndex= e.currentTarget.dataset.index;
    wx.switchTab({
      url: '/pages/order/order'
    })
  },
上一篇 下一篇

猜你喜欢

热点阅读