首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在存储有数据时调用函数

如何在存储有数据时调用函数
EN

Stack Overflow用户
提问于 2017-09-04 16:10:25
回答 1查看 1.6K关注 0票数 2

我必须创建一个视频播放器对象,但是在创建视频播放器之前,我需要流对象的存在。

this.stream由vuex数据存储填充。但是我发现mounted()created()方法不会等待存储数据的出现。

下面是Player.vue组件:

代码语言:javascript
复制
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'
          }
        });
      });
  }
};

有什么方法可以等待this.stream出现吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-04 21:12:49

您可以订阅突变事件,例如,如果您的商店中有一个名为setStream的突变,您应该订阅此突变。下面是如何在挂载方法中订阅的代码片段。

代码语言:javascript
复制
mounted: function () {
  this.$store.subscribe((mutation) => {
    if (mutation.type === 'setStream') {
      // Your player implementation should be here.
    }
  })
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46040832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档