1. 程式人生 > >求解:微信小程式的setData陣列動態賦值

求解:微信小程式的setData陣列動態賦值

資料編寫在utils目錄下的util裡面:

function getGoods(){

var goods = new Array();

var good = new Object();

good.id = '1000';//商品唯一識別符號

good.pic = '/images/pic/suanlatudousi.jpg';//商品圖片

good.name = '酸辣土豆絲';

good.price = '2';

good.address = '驪秀苑一樓';

goods.photos = ['/images/haibao/potato/1.jpg', '/images/haibao/potato/2.jpg'];

goods[0] = good;

var good1 = new Object();

good1.id = '1001';

good1.pic = '/images/pic/youpomian.jpg';

good1.name = '油潑面';

good1.price = '6';

good1.address = '驪秀苑一樓風味小吃區';

good1.photos = ['images/haibao/noodle/1.jpg', '/images/haibao/noodle/2.jpg'];

goods[1] = good1;

return goods;

}

module.exports = {

getGoods:getGoods

}

在index的js裡面的程式碼如下:

data: {

imgUrls:[

{imgsrc:'/images/swiper/1.jpg'},

{imgsrc:'/images/swiper/2.jpg'},

{imgsrc:'/images/swiper/3.jpg'},

{ imgsrc:'/images/swiper/4.jpg'}

],

goods:[]

},

/**

* 生命週期函式--監聽頁面載入

*/

onLoad: function (options) {

var id = options.id;

this.loadGoods(id);

},

loadGoods:function(id){

var goods = {}

var good = Goods.getGoods();

//this.setData({ goods:good[0]})

for (var i = 0; i < Goods.length; i++) {

good =Goods[i];

var page = this;

page.setData({goods:good});

}

},

在index裡面的wxml裡面的程式碼如下:

<view class='hr'></view>

<view class='list'>

<view class='good'>

<view class='pic'>

<view >

<image src='/images/pic/noodle.jpg' mode='aspectFit' style=' width:282px;height:200px;'></image>

</view>

<view class='movie-info'>

<view class='base-info'>

<view class='name'>{{goods.name}}</view>

<view class='people'><text class='price'>{{goods.price}}元</text></view>

</view>

<view class='btn'>立即購買</view>

</view>

</view>

</view>

<view class='hr'></view>

</view>

但是編譯後始終無法將值傳過來,求大神解惑。