简体   繁体   中英

VueJS - manipulate DOM after computed

I'm getting posts and comments from an API, using Vuex. Right now, I have:

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

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

The comments body is a string that contains HTML tags. I need to strip out the href attribute from a tags of a given class.

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

I'm not sure how to call cleanUnwantedLinks . It should be called just after the component is mounted (when I have the comments already). Is there a way to do it?

If you will return a promise from your getComments action, you could do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM