收藏
回答

tap 事件导致 页面data数据被清空

问题模块 框架类型 终端类型 操作系统 微信版本 基础库版本
框架 小程序 客户端 iOS 6.6 1.9.1


代码如下,

触发  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>


最后一次编辑于  2018-01-25  (未经腾讯允许,不得转载)
邀请回答
复制链接收藏投诉关注问题回答