cho*_*ovy 2 vue.js vue-component vuex vuejs2
我必须创建一个视频播放器对象,但是在创建视频播放器之前,我需要流对象存在。
该this.stream由vuex数据存储填充。但我发现mounted()and created()方法不会等待存储数据出现。
这是Player.vue组件:
import Clappr from 'clappr';
import { mapActions, mapGetters } from 'vuex';
import * as types from '../store/types';
export default {
name: 'streams',
data() {
return {
player: null
};
},
computed: {
...mapGetters({
stream: types.STREAMS_RESULTS_STREAM
}),
stream() {
return this.$store.stream;
}
},
methods: {
...mapActions({
getStream: types.STREAMS_GET_STREAM
})
},
mounted() {
this.getStream.call(this, {
category: this.$route.params.category,
slug: this.$route.params.slug
})
.then(() => {
this.player = new Clappr.Player({
source: this.stream.url,
parentId: '#player',
poster: this.stream.poster,
autoPlay: true,
persistConfig: false,
mediacontrol: {
seekbar: '#0888A0',
buttons: '#C4D1DD'
}
});
});
}
};
Run Code Online (Sandbox Code Playgroud)
有没有办法等待this.stream出现?
小智 5
您可以订阅突变事件,例如,如果您的商店中有一个突变名为,则setStream您应该订阅此突变。这是有关如何在已挂载方法中进行订阅的代码段。
mounted: function () {
this.$store.subscribe((mutation) => {
if (mutation.type === 'setStream') {
// Your player implementation should be here.
}
})
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
987 次 |
| 最近记录: |