在微信小程序中,是通过setData方法绑定数据的,代码如下所示
this.setData({title:'hello earth'});
WePY对setData进行了封装
可以更加简洁地修改数据实现绑定
不用重复写setData方法
示例代码如下
this.title = 'this is earth';
需要注意的是,在异步函数中更新数据的时候,必须手动调用$apply方法
<template><view><view>title在3s后发生变化:{{title}}</view></view></template><script>import wepy from 'wepy'export default class Demo1 extends wepy.page{data = {title: "变化之前的title"}onLoad(){setTimeout(() => {this.title = '变化之后的title';this.$apply();}, 3000);}}</script>
在示例6-1中,3s后把title改变成“变化之后的title”,页面显示效果如图6.9所示
