javascript - 微信小程序奇怪问题,数据获取不到
天蓬老师
天蓬老师 2017-04-11 09:09:09
[JavaScript讨论组]

1.微信小程序通过app.json去add page,其中pages中已添加"pages/monthDetail/index"页面,并且能从首页访问到该页面,静态页面完全OK。下一步上模拟数据。
2.通过以下数据来模拟真实数据,其中js中无其他ES6语法,此时,页面console提示
`

data: {
    yearList: [{
      year: '2016',
      totalIncome:'2016',
      monthList: [{
        month: '04',
        income: 233,
        cost: 23,
      },{
        month: '06',
        income: 232,
        cost:23,
      }]
    },{
      year: '2015',
      totalIncome:'2016',
      monthList: [{
        month: '12',
        income: 2323,
        cost: 111
      }]
    }]
  },

`
此时,页面console提示错误:

Page[pages/monthDetail/index] not found. May be caused by: 1. Forgot to add page route in app.json. 2. Invoking Page() in async task.

但是明明app.json里面已经添加过该页面,并且能访问得到,只是数据拿不到。app.json如下

{
  "pages":[    
    "pages/index/index",
    "pages/logs/logs",
    "pages/monthDetail/index"
  ],
  "window":{
    "backgroundTextStyle":"light",
    "navigationBarBackgroundColor": "#25282e",
    "navigationBarTitleText": "微信小程序",
    "navigationBarTextStyle":"#fff",
    "enablePullDownRefresh": true
  }
}

在wxml中代码如下:

<view wx:for-item="{{yearList}}">
  <view class="year box box-lr">
    <view class="box box-tb box-pack-center box-left">{{ item.year }}</view>
    <view class="box box-tb box-pack-center box-center">
      <view><view class="tip">收</view>:{{ item.totalIncome }}</view>
      <view><view class="tip">支</view>:{{ item.totalCost }}</view>
    </view>
    <view class="box box-tb box-pack-center box-right">
      <view>{{ item.totalIncome - item.totalCost }}</view>
      <view>结余</view>
    </view>
  </view>
  <view class="month box box-lr"  wx:for-item="{{item.monthList}}" wx:for-item="items">
    <view class="box box-tb box-pack-center box-left">{{ items.month }}</view>
    <view class="box box-tb box-pack-center box-center">
      <view><view class="income">收</view>:{{ items.income }}</view>
      <view><view class="cost">支</view>:{{ items.cost }}</view>
    </view>
    <view class="box box-tb box-pack-center box-right">
      <view>{{ items.income - items.cost }}</view>
      <view>结余</view>
    </view>
  </view>
</view>

3.有一起研究小程序的同伴大神遇到过这样的问题吗?

补充提问:

有看到大神遇到这样的情况,说是在app.json中二级页面的定义要在一级页面的下面。此方法也修改了,不报之前的console错误,但是数据依旧无法正常显示。
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(3)
天蓬老师
<view class="flow-outer" wx:for="{{yearList}}">
  <view class="year box box-lr">
    <view class="box box-tb box-pack-center box-left">{{ item.year }}</view>
    <view class="box box-tb box-pack-center box-center">
      <view><view class="tip">收</view>:{{ item.totalIncome }}</view>
      <view><view class="tip">支</view>:{{ item.totalCost }}</view>
    </view>
    <view class="box box-tb box-pack-center box-right">
      <view>{{ item.totalIncome - item.totalCost }}</view>
      <view>结余</view>
    </view>
  </view>
  <view class="month box box-lr"  wx:for="{{item.monthList}}" wx:for-item="items">
    <view class="box box-tb box-pack-center box-left">{{ items.month }}</view>
    <view class="box box-tb box-pack-center box-center">
      <view><view class="income">收</view>:{{ items.income }}</view>
      <view><view class="cost">支</view>:{{ items.cost }}</view>
    </view>
    <view class="box box-tb box-pack-center box-right">
      <view>{{ items.income - items.cost }}</view>
      <view>结余</view>
    </view>
  </view>
</view>

以上即为正确写法,有关于wx:for和wx:for-item以及wx:for-item="item"的正确用法

大家讲道理

我遇到过类似的问题,我的原因出在
用了ES6语法!
你检查下有没有用到ES6

PHP中文网

确保文件夹和文件名同名
那么路径应该是 pages/monthDetail/index/index 或者 pages/monthDetail/monthDetail
其次同名.js文件必须存在,并且调用

Page({})
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号