yun_ru 2022-05-09 22:37 采纳率: 0%
浏览 13

微信小程序渲染异常问题

问题遇到的现象和发生背景

我通过父页面向子页面传递了一个good-id参数,在子页面利用传进来的参数调用接口,显示good-id其他详细内容,返回数据显示正常,但是渲染出现错误,完全渲染不上任何内容,而且原本应该渲染一个,现在却渲染了20个

问题相关代码,请勿粘贴截图
子页面wxml
<block wx:for="{{showdata}}" wx:for-item="itemName" wx:for-index="id" wx:key="{{index}}"> 
    <view class="box">
        <view class="firstblock">
            <image class="touxiang" src="{{itemName.image_url}}" mode="widthFix"></image>
            <view class="person">
                <view class="name">
                    <text class="showname">{{itemName.seller_name}}</text>
                </view>
                <view class="name">
                    <text class="showname2">{{itemName.good_name}}</text>
                </view>
                
            </view>
        </view>
    <view class="secondblock">
        <text class="content"> {{itemName.content}}
</text>
    </view>
    <view class="thirdblock">     
        <view  class='del' bindtap='buy'  >购买</view>
    </view>
</view>
</block>



子页面js:
data: {
        showdata:{},
        

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that=this
        
      that.setData({
        good_id:options.good_id
      })
      console.log(options.good_id)
      
      
      
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
          url: getApp().globalData.server +'/index.php/Home/Goods/get_one_lol_goods', //仅为示例,并非真实的接口地址
          data: {
            good_id:options.good_id       
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          method:'POST',
          success (res) {
            console.log(res.data)
           
           if(res.data.error_code!=0){
            wx.showModal({
              title: '哎呀!',
              content: '出错了呢'+res.data.msg,
              showCancel:false,
              success (res) {}
            })
          }
          else if(res.data.error_code==0){
              that.setData({
                 showdata:res.data.data
              })
              
              console.log(that.data.showdata)  
                                                          
        }
              },
              fail:function (res) {
                wx.showModal({
                  title: '哎呀!',
                  content: '网络不在状态呢',
                  success :function (res) {
                    if(res.confirm){
                      console.log('用户点击确定')
                    }else if(res.cancel){
                      console.log('用户点击取消')
                    }
                  }
                    })
                  },
          complete:function (res) {
              wx.hideLoading()
          }
        })
        setTimeout(function(e){
          wx.hideLoading()
      },2000)



    },

运行结果及报错内容

img

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

1条回答 默认 最新

  • 前端陈皮喵 2022-05-10 15:02
    关注

    提个建议,目前排查如果返回的数据没有问题,正常情况下根据id返回的数据就是只有一条,那就是wx:for循环那里的问题了,可以试试下面的方法一般是不会出现问题的,使用wx:for-item也需谨慎!

    <block wx:for="{{showdata}}" wx:key="{{index}}"> 
        <view class="box">
            <view class="firstblock">
                <image class="touxiang" src="{{item.image_url}}" mode="widthFix"></image>
                <view class="person">
                    <view class="name">
                        <text class="showname">{{item.seller_name}}</text>
                    </view>
                    <view class="name">
                        <text class="showname2">{{item.good_name}}</text>
                    </view>
                    
                </view>
            </view>
        <view class="secondblock">
            <text class="content"> {{item.content}}
    </text>
        </view>
        <view class="thirdblock">     
            <view  class='del' bindtap='buy'  >购买</view>
        </view>
    </view>
    </block>
    
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 5月9日

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条