代码如下,
触发 tapFirstClass 事件时,定义的 sourceData、firstClassOpts、secondClassOpts 对象会变为空数组,为什么?
<template>
<view class="page">
<view class="class-wrap">
<view class="{{secondClassOpts.length>0?'class-first-wrap':'class-first-wrap-full'}} weui-cells weui-cells_after-title">
<view bindtap="tapFirstClass" data-firstClass="{{item}}" data-sourceData="{{sourceData}}" class="weui-cell" wx:for="{{firstClassOpts}}" wx:key="idx" wx:for-item="item">
<view class="weui-cell__bd">{{item.name}}</view>
</view>
</view>
<view wx:if="{{secondClassOpts.length > 0}}" class="class-second-wrap weui-cells weui-cells_after-title">
<view class="weui-cell" wx:for="{{firstClassOpts}}" wx:key="idx" wx:for-item="item">
<view class="weui-cell__bd">{{item.name}}</view>
</view>
</view>
</view>
</view>
</template>
<script>
import wepy from 'wepy';
export default class reportFirstStep extends wepy.page {
config = {
navigationBarTitleText: '第一步 选择大小类'
};
data = {
sourceData: [],
firstClassOpts: [],
secondClassOpts: []
};
onLoad() {
let that = this;
wx.request({
url:
'http://127.0.0.1:8080/class/findAll?typeId=1',
method: 'GET',
success: function(res) {
if (res.statusCode == 200 && res.data.code == 200) {
let opts = [];
// 获取大类
for (let i = 0; i < res.data.data.length; i++) {
if (res.data.data[i].parentClassId == 0) {
opts.push(res.data.data[i]);
}
}
that.setData({ sourceData: res.data.data });
that.setData({ firstClassOpts: opts });
}
}
});
}
tapFirstClass(event) {
let that = this;
debugger;
let firstClass = event.currentTarget.dataset.firstclass;
let sourceData = event.currentTarget.dataset.sourcedata;
let opts = [];
// 获取小类
for (let i = 0; i < sourceData.length; i++) {
if (sourceData[i].parentClassId == firstClass.id) {
opts.push(sourceData[i]);
}
}
this.setData({ secondClassOpts: opts });
}
}
</script>