收藏
回答

Template页嵌套循环,无法正常传递数据?

引用template代码

<view wx:for="{{items.children}}" wx:key="id" wx:for-index="cindexs" wx:for-item="items">
  <template is="CommentItem" data="{{item,items,cindexs,cindexs}}"></template>
</view>

template代码如下

<template name="CommentItem">
    <--省略部分-->
    <block wx:if="{{items.children}}">
        <view wx:for="{{items.children}}" wx:key="id" wx:for-index="cindexs" wx:for-item="items">
            <template is="CommentItem" data="{{item,items,cindexs,cindexs}}"></template>
        </view>
    </block>
</template>

其中嵌套部分无法正常显示数据,不知道是哪里出现了问题?

===========================================================

目前解决方法是写多个<template>,修改名字:

<template name="CommentItem">
  <--省略部分-->
  <block wx:if="{{items.children}}">
        <view wx:for="{{items.children}}" wx:key="id" wx:for-index="cindexs" wx:for-item="items">
            <template is="CommentItem1" data="{{item,items,cindexs,cindexs}}"></template>
        </view>
    </block>
</template>


<template name="CommentItem1">
  <--省略部分-->
  <block wx:if="{{items.children}}">
        <view wx:for="{{items.children}}" wx:key="id" wx:for-index="cindexs" wx:for-item="items">
            <template is="CommentItem2" data="{{item,items,cindexs,cindexs}}"></template>
        </view>
    </block>
</template>


<template name="CommentItem2">
  <--省略部分-->
  <block wx:if="{{items.children}}">
        <view wx:for="{{items.children}}" wx:key="id" wx:for-index="cindexs" wx:for-item="items">
            <template is="CommentItem3" data="{{item,items,cindexs,cindexs}}"></template>
        </view>
    </block>
</template>

<--以此类推-->

源码如下:

<template name="CommentItem">
    <view class="comment-header">
        <view class="comment-header-left" catchtap="userTap" data-uid="{{items.user}}">
            <image class="comment-avatar" src="{{items.user_avatar}}">
            </image>
            <view class="comment-user-date">
                <text>{{items.user_nickname}}</text>
                <text>{{items.create_time}}</text>
            </view>
        </view>
        <view class="comment-header-right">
            <view>{{filters.toFix(items.like_count)}}</view>
            <image data-id="{{items.id}}" data-cmtindex="{{cindex}}" data-cmtindexs="{{cindexs}}" bindtap="tapCommentslike" src="/image/heart-fill.png" wx:if="{{items.is_like}}">
            </image>
            <image data-id="{{items.id}}" data-cmtindex="{{cindex}}" data-cmtindexs="{{cindexs}}" bindtap="tapCommentslike" src="/image/heart-line.png" wx:else></image>
            <text wx:if="{{items.user == item.current_user_id}}" data-id="{{items.id}}" data-cmtindex="{{cindex}}" data-cmtindexs="{{cindexs}}" bindtap="tapDeleteComments">删除</text>
        </view>
    </view>
    <view class="comment-content" bindtap="tapComment" data-id="{{item.id}}" data-reply="{{items.user_nickname}}" data-userid="{{items.user}}">
        <view class="commentconent">
            <view class="contentinfo">
                <block wx:if="{{item.user != items.user}}">
                    回复<text>{{item.user_nickname}}</text>:
                </block>
                <block wx:if="{{items.content != null}}">
                    {{items.content}}
                </block>
            </view>
        </view>
        <image wx:if="{{items.image != null && items.image != ''}}" class="img-list" lazy-load='{{true}}' catchtap="previewImgFunc" data-list="{{items.imgList}}" data-src="{{items.image}}" mode="aspectFill" src="{{items.image}}"></image>
    </view>
    <block wx:if="{{items.children}}">
        <view wx:for="{{items.children}}" wx:key="id" wx:for-index="cindexs" wx:for-item="items">
            <template is="CommentItem" data="{{item,items,cindexs,cindexs}}"></template>
        </view>
    </block>
</template>
回答关注问题邀请回答
收藏

2 个回答

  • Demons
    Demons
    2023-04-08

    请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

    2023-04-08
    有用
    回复 1
    • 小学究
      小学究
      2023-04-08
      在下面的template中引用了名字叫CommentItem的template,名字叫CommentItem的template源码在问题下面的源码中
      2023-04-08
      回复
  • 那一抹笑😃 穿透阳光
    那一抹笑😃 穿透阳光
    2023-04-08

    添加一个代码片段嘛,你这个写的,不好找问题在哪里

    2023-04-08
    有用
    回复 1
    • 小学究
      小学究
      2023-04-08
      template源码在问题里,在下面😃
      2023-04-08
      回复
登录 后发表内容