我想迭代一个TextTrackCueList
元素,它基本上是HTML5视频标题的数组(参考:https://developer.mozilla.org/en-US/docs/Web/API/TextTrack#Properties)。下面是简化代码:
new Vue ({
el: "#app",
data() {
return {
vid: null,
track: null,
cues: []
}
},
mounted() {
this.vid = this.$refs.vid;
this.track = this.vid.addTextTrack("captions");
this.track.mode = "showing";
this.cues = this.track.cues;
this.addCue(); //We add just one cue before the list is rendered
},
methods: {
addCue() {
let i = this.cues.length;
//The cue just shows during one second
let cue = new VTTCue(i, i+1, "Caption "+i);
this.track.addCue(cue);
}
}
});
.cues-list {
float: right;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<video ref="vid" width="50%" src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4" controls></video>
<div class="cues-list">
<ul>
<li v-for="cue in cues">
{{ cue.text }}
</li>
</ul>
<button @click="addCue">Add cue</button>
</div>
</div>
cues
列表没有更新。我怀疑的原因是我没有使用Vue.js覆盖的变异方法之一(https://v2.vuejs.org/v2/guide/list.html#Mutation-Methods),因此DOM不会自动更新。实际上,我正在使用TextTrack.addCue()
来添加提示,而不是Array.push()
,因为TextTrack.cues
属性是只读的。是否有一种解决方法,例如手动更新虚拟DOM的方式?感谢您的帮助。