Home  >  Q&A  >  body text

vue data is not updated but the data changes

I'm crazy about this.

This is the code, the component I'm using in my form to display a video preview when pasting a url into the input:

<template>
  <div class="row">
    {{embedData}}
    <input v-model="embedData" name="content[body]" id="content_body">
    <div class="col-md-6">
      <div class="form-group">
        <div class="form-group url optional content_video_url form-group-valid">
          <label for="content_video_url" class="url optional">Url del video</label>
          <input @change="forceRerender" v-model="url" type="url" value="" name="content[video_url]" id="content_video_url" class="form-control is-valid string url optional">
        </div>
      </div>
    </div>
    <div class="col-md-6">
      <div class="video-responsive"
        <o-embed ref="embed" :url="url" :key="componentKey"></o-embed>
      </div>
    </div>

  </div>
</template>

<script>

import oEmbed from './oEmbed'
import EventBus from '../utils/eventBus'

export default {
  components: {
    oEmbed
  },

  props: {
    video_url: String,
    video_caption: String
  },

  created: function() {
    this.url = this.video_url;
    this.caption = this.video_caption;
  },
  mounted: function() {
    EventBus.$on('HTML', function (payLoad) {
      this.embedData = payLoad
      console.log('payLoad:' + this.embedData);
      console.log('arrived');
    });
  },
  data: function() {
    return {
      url: '',
      caption: '',
      componentKey: 0,
      embedData: ''
    }
  },
  methods: {
    forceRerender () {
      this.componentKey = this.componentKey + 1;
    }
  }
}
</script>

o-embed is a component. I added a simple bus emission function when the component is updated:

mounted: function() {
    EventBus.$on('HTML', function (payLoad) {
      this.embedData = payLoad
      console.log('payLoad:' + this.embedData);
    });
  }

If I check the console log I get this

payLoad: <iframe src="https://player.vimeo.com/video/596287904?app_id=122963&amp;h=d77f5dc57c" width="426" height="240" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen title="Raritan Bay Cruisers-Hopelawn New Jersey CruisebNight 8-31-2021.wmv"></iframe>

Everythink is working, this.embedData looks fine and I have the logs, but when I render embedData in my view it is empty.

I've included an additional info: I'm forcing a re-rendering of the embedded component, but I don't think it's relevant.

Any ideas?

P粉071626364P粉071626364245 days ago427

reply all(2)I'll reply

  • P粉006540600

    P粉0065406002024-02-18 12:05:48

    Mythos discovered this problem (at least one of them). Mustache templates (double curly braces) interpret content as plain text, not html. If you want to inject raw html into your page you should do this

    Instead (https://v2.vuejs.org/v2 /guide/syntax.html#Raw-HTML)

    reply
    0
  • P粉998920744

    P粉9989207442024-02-18 09:06:46

    You are using an anonymous function. this The inner anonymous function does not provide the context of the component.

    Try using arrow functions:

    EventBus.$on('HTML', (payLoad) => {
          this.embedData = payLoad
          console.log('payLoad:' + this.embedData);
        });

    reply
    0
  • Cancelreply