VueJS - 计算后操作DOM

时间:2017-07-19 21:36:05

标签: javascript vue.js vuex

我使用Vuex从API获取帖子和评论。现在,我有:

  mounted () {
    this.$store.dispatch('getComments', this.post);
  },

  computed: {
    comments () {
      return this.$store.getters.orderedComments;
    },

评论正文是一个包含HTML标记的字符串。我需要从给定类的href标记中删除a属性。

  cleanUnwantedLinks () {
    const link = document.getElementsByClassName('link-class')[0];
    link.removeAttribute('href');
  }

我不确定如何致电cleanUnwantedLinks。应该在安装组件之后调用它(当我已经有注释时)。有办法吗?

1 个答案:

答案 0 :(得分:0)

如果您要从getComments行动中退回承诺,则可以执行以下操作:

this.$store
  .dispatch('getComments', this.post)
  .then(() => {
    cleanUnwantedLinks()
    /* 
    Can also try wrapping your call in $nextTick
    this.$nextTick(() => { cleanUnwantedLinks() })
    */
  });