问题引入:
今天在学view组件时,遇到了这个问题。本想通过js来动态控制播放暂停,同时在暂停时添加一个遮罩层(动态控制该遮罩层的display样式),然而在用 this.data.videoStatus=false
方式时,视图没有发生变换并且 样式没有被修改。
缘由:
this.setData({})
方式,会将数据从逻辑层发发送到视图层,而 this.data
方式不会同步数据(没有更新视图层的数据)。
在小程序文档中也有说明:
viewTap: function() {
this.setData({
text: 'Set some data for updating view.'
}, function() {
// this is setData callback
})
},
部分代码:
<view class="container">
<video id="myvideo" src="xxxx" >
<cover-view class="videomask" style="display:{{ videoStatus?'none':'flex' }}">
<button id="playbtn" bindtap="bindplay" >播放</button>
</cover-view>
</video>
</view>
onLoad: function (options) {
this.videoContext = wx.createVideoContext('myvideo')
},
bindplay(){
this.videoContext.play()
//无法更新view层的的数据,应该使用下面的方式
this.data.videoStatus=true
},
应更改为:
onLoad: function (options) {
this.videoContext = wx.createVideoContext('myvideo')
},
bindplay(){
this.videoContext.play()
this.setData({
videoStatus:true
})
},
初次接触微信小程序开发,如果讲解有误,请在评论区指出。