admin管理员组文章数量:1297247
I'm using Vuejs to make a control panel for audio, I want to get the currentTime
property bind to a computed
value, so I write
computed: {
'currentTime': {
cache: false,
get: function () {
return document.getElementById('player').currentTime
}
}
},
and here is my audio
tag:
<audio :src="musicSrc" preload="auto" id="player">
<p>Your browser does not support the <code>audio</code> element.</p>
</audio>
I can get it in ready
:
ready () {
this.player = document.getElementById('player')
},
I can control it in methods
play: function () {
this.player.play()
},
But when I use {{ currentTime }}
in template I got
Error when evaluating expression "currentTime".
Uncaught TypeError: Cannot read property 'currentTime' of null
I'm using Vuejs to make a control panel for audio, I want to get the currentTime
property bind to a computed
value, so I write
computed: {
'currentTime': {
cache: false,
get: function () {
return document.getElementById('player').currentTime
}
}
},
and here is my audio
tag:
<audio :src="musicSrc" preload="auto" id="player">
<p>Your browser does not support the <code>audio</code> element.</p>
</audio>
I can get it in ready
:
ready () {
this.player = document.getElementById('player')
},
I can control it in methods
play: function () {
this.player.play()
},
But when I use {{ currentTime }}
in template I got
Share Improve this question asked Mar 1, 2016 at 1:26 zl2003cnzl2003cn 4851 gold badge7 silver badges23 bronze badges 2 |Error when evaluating expression "currentTime".
Uncaught TypeError: Cannot read property 'currentTime' of null
4 Answers
Reset to default 13For vue 2.0 the way to reference an element has changed.
In HTML
<audio ref="audio" controls>
In Vuejs
this.currentTime = this.$refs.audio.currentTime
You should register via v-el to reference it
<audio v-el:audio :src="musicSrc" preload="auto"></audio>
then access it in you vue instance via $els property, like so
ready: function() {
this.$els.audio.play();
}
It seems a particularly "vue" way to approach this is like so:
HTML:
<audio @timeupdate='onTimeUpdateListener'></audio>
JS:
export default {
data () {
return {
currentTime: '00:00' // The initial current time
}
},
methods: {
onTimeUpdateListener: function() {
// Update current time
this.currentTime = this.$els.audio.currentTime
}
}
}
Credit for this approach goes to the author of this SO answer.
In Vue 3. this.$refs is no longer available in composition API. You'll need to use the ref() class instead.
<audio ref="audio"></audio>
import { ref } from vue;
export default {
setup() {
const audio = ref(null);
....
return {
audio
}
}
本文标签: javascriptvuejs with HTML5 audio propertyStack Overflow
版权声明:本文标题:javascript - vuejs with HTML5 audio property - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738498854a2090148.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
this.currentTime
? – Yerko Palma Commented Mar 1, 2016 at 14:23