小程序的后端代码语法进阶
官方文档:
https://developers.weixin.qq.com/miniprogram/dev/wxcloud/basis/getting-started.html
首先,先使用云开发提供的 API 获取数据库:
const db = wx.cloud.database()
随后我们可以对数据库中的数据进行增删改查:
onLoad() { // 使用 promise 风格 db.collection('instruction').get().then(res => { // 获取 instruction 集合中的数据 console.log(res) // 数据绑定 this.setData({ insList: res.data }) }) }
随后利用 wx:for 渲染到页面上即可,例如:
<view class="gridContainer"> <block wx:for="{{insList}}" wx:key="index"> <!-- 判断是否被点击过 --> <view class="{{item.click ? 'box boxColor2':'box boxColor1'}}" bindtap="boxClick" data-index="{{index}}"> <text>{{item.name}}</text> </view> </block> </view>
其中,data- 属性用于传参,在点击事件中,我们可以使用 e.currentTarget.dataset 中获取数据(注意 e.target.dataset 与此略有不同)