小程序学习:两种数据加载方式

第一种

静态数据直接写入 index.js:

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    text: 'hello china!',
    array: ['明修栈道', '暗度陈仓']
  },

修改index.wxml:

  <text style='color:red'>{{text}}</text>
  <text>{{array[0]}}:{{array[1]}}</text>

结果:

第二种:

修改 index.js

  onLoad: function () {
    var content = {
      test1:"鱼戏莲叶间", test2: "鱼戏莲叶东"
    }
    this.setData(content);

修改 index.wxml:

  <text>{{test1}},{{test2}}</text>

运行结果:

 

继续修改:

  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    text: 'hello china!',
    array: ['明修栈道', '暗度陈仓'],
    poem: [{
      text: '离离原上草',
      author: '白居易'
    }, {
        text: '慈母手中线',
        author: '孟郊'
    }]
  },
.usermotto {
  margin-top: 200px;
}

.poem {
  margin-top: 20px;
}
  <view class="poem">
    <text class="user-motto">{{motto}}</text>
  </view>

  <view wx:for="{{['青青园中葵','朝露待日晞']}}">
    {{item}}
  </view>

  <view class="poem" wx:for="{{poem}}"> 
    {{item.text}}
  </view>

还可以这样:

  <view class="poem" wx:for="{{poem}}" wx:for-item='item' wx:for-index='key'> 
    {{item.text}}:{{key}}
  </view>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值