所以我设法注入 hls.js 来使用 nuxtjs $root 元素和this
我是这样做的(hls.client.js):
import Hls from 'hls.js';
export default (context, inject) => {
inject('myhls', Hls)
}
和 nuxt.config.js
plugins: [
'~plugins/hls.client.js',
],
从字面上看,这很好用:-) 但我不能在 hls 事件中引用“this”。
playHls() {
this.playing = true
if(this.$myhls.isSupported()) {
this.hls = new this.$myhls();
this.audio = new Audio('');
this.hls.attachMedia(this.audio);
this.hls.loadSource(this.scr_arr[2]);
this.audio.play()
// 'THIS' DOES NOT WORK INSIDE this.hls.on
this.hls.on(this.$myhls.Events.MEDIA_ATTACHED, function () {
console.log(this.scr_arr[2]); // DOES NOT LOG
console.log("ok") // works great
});
// 'THIS' DOES NOT WORK INSIDE this.hls.on
this.hls.on(this.$myhls.Events.MANIFEST_PARSED, function (event, data) {
console.log('manifest loaded, found ' + data.levels.length + ' quality level') // WORKS
console.log("ok") // WORKS
this.audio.volume = 1 // DOES not work
});
}
},
所以我在这些事件中我不能使用 nuxtjs 'this',因为似乎有不同的范围?
我可以在这些事件中以某种方式获得“这个”nuxt 范围吗?