微信小程序吧 关注:57,759贴子:703,952
  • 19回复贴,共1

有大神能帮看下吗 获取不到数据

只看楼主收藏回复






IP属地:北京1楼2017-02-11 18:48回复
    this.setData({
    postList:postsData.postList
    });
    用这个能获取到数据 而用 this.data.postList = postsData.postList 就获取不到,但是教程的源码就是第二个,谁能帮帮我啊 卡了一下午了


    IP属地:北京2楼2017-02-11 18:50
    回复
      2025-07-24 14:59:22
      广告
      不感兴趣
      开通SVIP免广告
      截图的代码中看不到你的postsData.postList是从何而来,报错是说不存在postList这个属性,怀疑你postsData这个对象压根就不存在,你console.log(postsData) ,是undefined就代表,压根就不存在这个对象,自然没有postList这个属性,你去使用他,也就报错了;
      二楼的问题是setData能获取到数据,后面赋值不能,不知道你的实际情况,我的理解是用前者,你能正常显示页面,用后者,页面就无法显示,如果是这样的话:小程序页面变量的渲染,必须要用setData,前台才会进行渲染。 后面的赋值,只是改变值而已,并没有让前台进行渲染,简单的说,如果要让前台页面发生改变,就必须setData


      IP属地:浙江3楼2017-02-11 23:40
      收起回复
        var postsData = require('../../../data/posts-data.js')
        Page({
        data:{},
        onLoad:function(option){
        // 页面初始化 options为页面跳转所带来的参数
        var postId=option.id;
        var postData=postData.postList[postId];
        console.log(postData);
        //this.data.postData=postData;
        this.setData({
        postData:postData
        })
        },
        onReady:function(){
        // 页面渲染完成
        },
        onShow:function(){
        // 页面显示
        },
        onHide:function(){
        // 页面隐藏
        },
        onUnload:function(){
        // 页面关闭
        }
        })


        IP属地:北京4楼2017-02-12 00:30
        回复
          var local_database=[
          {
          date:"Sep 22",
          title:"hahahahh",
          imgSrc:"/images/1.jpg",
          content:"1啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊",
          reading:"112",
          collection:"96",
          avatar:"/images/1.jpg",
          postId:0,
          dataTime:"24小时",
          headImgSrc:"/images/1.jpg",
          detail:"xixixxixiiixxiixiiixixixi",
          author:"深白色"
          },
          {
          date:"Sep 22",
          title:"hahahahh",
          imgSrc:"/images/1.jpg",
          content:"1啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊",
          reading:"112",
          collection:"96",
          avatar:"/images/1.jpg",
          postId:1,
          dataTime:"24小时",
          headImgSrc:"/images/1.jpg",
          detail:"xixixxixiiixxiixiiixixixi",
          author:"深白色"
          },
          {
          date:"Sep 22",
          title:"hahahahh",
          imgSrc:"/images/1.jpg",
          content:"1啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊",
          reading:"112",
          collection:"96",
          avatar:"/images/1.jpg",
          postId:2,
          dataTime:"24小时",
          headImgSrc:"/images/1.jpg",
          detail:"xixixxixiiixxiixiiixixixi",
          author:"深白色"
          },
          {
          date:"Sep 22",
          title:"hahahahh",
          imgSrc:"/images/1.jpg",
          content:"1啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊",
          reading:"112",
          collection:"96",
          avatar:"/images/1.jpg",
          postId:3,
          dataTime:"24小时",
          headImgSrc:"/images/1.jpg",
          detail:"xixixxixiiixxiixiiixixixi",
          author:"深白色"
          },
          {
          date:"Sep 22",
          title:"hahahahh",
          imgSrc:"/images/1.jpg",
          content:"1啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊",
          reading:"112",
          collection:"96",
          avatar:"/images/1.jpg",
          postId:4,
          dataTime:"24小时",
          headImgSrc:"/images/1.jpg",
          detail:"xixixxixiiixxiixiiixixixi",
          author:"深白色"
          }
          ]
          module.exports={
          postList:local_database
          }


          IP属地:北京5楼2017-02-12 00:31
          回复
            <!--先静后动,先样式再数据-->
            <view class="container">
            <image class="head-image" src="/images/1.jpg"></image>
            <image catchtap="onMusicTap" class="audio" src="/images/1.jpg"></image>
            <view class="author-date">
            <image class="avatar" src="{{postData.avatar}}"></image>
            <text class="author">{{postData.author}}</text>
            <text class="const-text">111</text>
            <text class="date">{{postData.dateTime}}</text>
            </view>
            <text class="title">{{postData.title}}</text>
            <view class="tool">
            <view class="circle-img">
            <image wx:if="{{collected}}" catchtap="onColletionTap" src="/images/1.jpg"></image>
            <image wx:else catchtap="onColletionTap" src="/images/1.jpg"></image>
            <image catchtap="onShareTap" class="share-img" src="/images/1.jpg"></image>
            </view>
            <view class="horizon"></view>
            </view>
            <text class="detail">{{postData.detail}}</text>
            </view>


            IP属地:北京6楼2017-02-12 00:32
            回复
              <import src="post-item/post-item-template.wxml" />
              <view>
              <swiper indicator-dots="true" autoplay="true" interval="2000">
              <swiper-item><image src="/images/0.jpg" ></image></swiper-item>
              <swiper-item><image src="/images/1.jpg" ></image></swiper-item>
              <swiper-item><image src="/images/2.jpg" ></image></swiper-item>
              </swiper>
              <block wx:for="{{postList}}" wx:for-item="item" wx:for-index="idx">
              <view catchtap="onPostTap" data-postId="{{item.postId}}">
              <template is="postItem" data="{{...item}}"/>
              </view>
              </block>
              </view>


              IP属地:北京7楼2017-02-12 00:33
              回复
                我data好像丢了个s
                我这次全用的set也算是解决了,数据全拿出来了,但是这算不算报错?我一直不理解用那种this.data.postList = postsData.postList 这种设置属性的方法在我这不行,视频上就行,难道更新啦的原因吗


                IP属地:北京8楼2017-02-12 01:14
                收起回复
                  2025-07-24 14:53:22
                  广告
                  不感兴趣
                  开通SVIP免广告
                  用setdata,别当vue用


                  IP属地:广东来自iPhone客户端9楼2017-03-03 13:10
                  回复
                    这又是一个很无理的改动,应该是更改了绑定数据的时机。之前版本,在onLoad函数里不需要使用this.setData方法来做数据更新,只需要使用this.data = { key:value} 即可更新数据绑定变量。这样做的方法我在课程里也分析过,是因为onLoad函数执行是在数据绑定初始化之前。
                    但此次更新后这种方法失效了,造成文章页面无法正常初始化数据。请同学们将所有用this.data做数据绑定的地方,更新成this.setData问题即可解决


                    IP属地:广东10楼2017-08-06 19:43
                    收起回复
                      楼主好,我现在是学生,想自学点微信小程序,从哪里可以免费学啊?


                      IP属地:上海来自Android客户端12楼2018-05-24 21:06
                      收起回复
                        这个问题我记得解决了 忘了,早不学小程序了 汗。。。。。


                        IP属地:北京13楼2018-08-06 12:24
                        回复