VueJS - 计算后操作 DOM

VueJS - manipulate DOM after computed

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

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

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

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

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

我不确定如何调用 cleanUnwantedLinks。它应该在组件安装后立即调用(当我已经有评论时)。有办法吗?

如果您要 return 从您的 getComments 行动中得到承诺,您可以:

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