获得服务器数据后无法正常绑定到data

来源:12-1 图书组件

39sakuchan

2019-06-24

我想问下,给data.books绑定完数据后 打印books为空没绑定上,在onReady里打印也为空,最后在onLoad里打印出来了,但是页面没有绑定上数据,请问下老师是怎么回事,谢谢
图片描述

> <view class='container'>
>   <image src="{{book.image}}"></image>
>   <view class='description'>
>     <text class='title'>{{book.title}}</text>
>     <text class='author'>{{book.author}}</text>
>     <view>
>       <text class='footer'>{{book.fav_nums}} 喜欢</text>
>     </view>
>   </view>
> </view>

> import {
>   BookModel
> } from '../../models/book.js'
> 
> const bookModel = new BookModel()
> 
> Page({
> 
>   /**
>    * 页面的初始数据
>    */
>   data: {
>     books:[],
>   },
> 
>   /**
>    * 生命周期函数--监听页面加载
>    */
>   onLoad: function(options) {
>     bookModel.getHotList()
>     .then(res => {
>           this.setData({
>             books: res
>           })
>       console.log(this.data.books)
>         }
>       )
>     console.log(this.data.books)
> 
>   },
> import {
>   HTTP
> } from '../util/http-p.js'
> 
> class BookModel extends HTTP {
>   getHotList() {
>     return this.request({
>       url:'book/hot_list'
>       })
>   }
> 
>   getMyBookCount(){
>     return this.request({
>       url:'/book/favor/count'
>     })
>   }
> }
> 
> export {
>   BookModel
> }
写回答

2回答

7七月

2019-06-24

可能的原因:setData是异步的操作,你setData后马上打印this.data并不一定异步就执行完成了。

0
3
39sakuchan
非常感谢!
2019-06-25
共3条回复

39sakuchan

提问者

2019-06-25

忘设置组件属性了 我擦

0
0

纯正商业级微信小程序应用实战,全方位小程序特性讲解

真实数据的高质量小程序项目,学会直接工作

4782 学习 · 2125 问题

查看课程