Vue data remains the same despite updates in data

Solving this puzzle is driving me insane.

Let's take a look at the code. This snippet represents a component used within a form to display a video preview when a URL is pasted into an input field:

<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">Video URL</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 separate component, and I've included a basic event bus emit function that triggers when the component updates:

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

Upon checking the console log, the output is as follows:

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>

Everything appears to be functioning correctly. this.embedData contains the expected data in the log. However, when trying to render embedData in the view, it shows up empty.

As an additional detail, although I am forcing a re-render of the embed component, I suspect this issue may not be directly related to that. Any thoughts or suggestions on what might be causing this?

Answer №1

You have employed an anonymous function. this within the anonymous function does not access the context of the component.

Consider utilizing an arrow function instead:

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

Answer №2

Mythos uncovered an issue, one of them at least. The mustache template, with double curly braces, is interpreting contents as plain text rather than HTML. If you need to insert raw HTML into your page, you should consider using something like:

<div v-html="embedData"></div>

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

jQuery Waypoints does not activate

I am encountering an issue with using jQuery Waypoints on a website I am currently developing. Despite my efforts, I cannot seem to get it to trigger at all. Below is the code I have implemented: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11 ...

Issues with loading CSS and JS resources in vue.js running on Kubernetes, encountering a 404 error

We are facing an issue with getting our vue.js application to work on Kubernetes (running in a container using nginx). We are using ingress to route to the site, and below is our configuration: Dockerfile: # build stage FROM public.ecr.aws/docker/library/ ...

Error message displayed by console when attempting to parse JSON data that is in array

After receiving a simple array as my JSON response: [35,55] The Chrome network inspector confirms that it is valid JSON. However, when attempting to parse the xhr.responseText using JSON.parse, I am confronted with the error: Uncaught SyntaxError: Unexpe ...

Executing several AJAX functions within a single page of functions

Is there a way to consolidate all database queries onto a single page at the root of each web section? This would help streamline the process and improve efficiency. Currently, I am using a script that allows SQL searches to appear as the user types. Here ...

Deactivate button after a set period of time

Using angularjs, I have a button to create tickets that works with an http request to call a PHP web service. The problem is that the http request has a delay of x seconds, allowing users to click multiple times during this delay resulting in multiple ti ...

Any suggestions on how to incorporate the variable into the inline JavaScript [[]] API path?

I have a query regarding creating a URL in JavaScript, but I'm unsure how to include variables within th:inline="javascript". Below is my code snippet: <script th:inline="javascript"> $(function() { $('#querySubmit').click(queryS ...

Populate table cells dynamically using an array of elements

I am trying to dynamically change the background color of table cells based on their values. Here is an example of the code I have written: applySchedules = function(schedules){ $.map(schedules, function(value, index){ $('#'+va ...

Upon refreshing the page, next.js 13's useSession() function fails to fetch session information

Currently, I am working on replicating an ecommerce site using nextjs 13. On the order page, I am utilizing useSession from next-auth/react to check if a user is signed in or not. Everything works fine when I navigate to the page through a link, but if I r ...

Disallow standard click behavior if a specific class is present elsewhere on the webpage

My CSS code triggers a dropdown menu to open when the .dashboard-actions class is clicked. When the user clicks, the class of the 2nd div in the tree changes to "dropdown open", indicating that the dropdown menu is visible. Clicking off the menu removes th ...

AngularJS - Ng-Table - Undefined

I'm facing difficulties when trying to create an ngTable. I can successfully render manually created arrays, but when I connect it to my api, it fails without any error message which is confusing. The $scope.data array is being filled as expected, but ...

Insert multiple text box values as a new entry in an SQL database

Currently, I am implementing a code snippet to incorporate additional text boxes within a form. The main purpose is to enable users to input multiple languages they are proficient in. <script> jQuery(function($) { var i = 0; ...

What could be causing the additional iteration in my for loop and preventing it from copying to the intended cells?

Every time I execute my script, it seems to run one additional time than necessary. For instance, it generates a PDF and marks a cell as processed below the last row. Another issue is that the URLs do not align correctly with their corresponding names. I ...

Showing options in a menu to choose from

I'm currently working on implementing a dropdown menu for a news website. When the user selects '1' from the dropdown list, the page should display content corresponding to that selection. If the user selects '2', the page ...

Arranging an Array of Objects in JavaScript by dual criteria

In my JavaScript code, I have an object structured like this: myArray[0] -> 0:"62", 1:8, 2:0, 3:"11" myArray[1] -> 0:"62", 1:8, 2:0, 3:"15" myArray[2] -> 0:"48", 1:8, 2:0, 3:"04" myArray[3] -> 0:"48", 1:8, 2:0, 3:"01" myArray[4] -> 0:"62", ...

Hold on for the completion of Angular's DOM update

I am facing an issue where I need to connect the bootstrap datepicker to an input field generated by AngularJS. The typical approach of using jQuery to attach the datepicker doesn't work as expected because the element is not yet available: $(functi ...

Combine the arrays to utilize ng-repeat

I'm working with several arrays and I need to display them in a table using ng-repeat. Is there a way to combine arrays like name=[name1, name2] and type=[type1, type2] into a single array data=[..] so that I can use ng-repeat for data.name and data.t ...

FancyBox: Can You Achieve the Same Scrolling Effect with HTML Fragments Instead of Images?

Is it possible to display HTML fragments instead of images with the same scrolling effect (when using the next() method)? I am struggling to figure out how to make this work. Currently, I am able to display a single HTML fragment using fancybox: $.fancyb ...

Chrome extension: some APIs disappear after chrome.runtime.reload() is called

Issue with my Chrome Extension involving the chrome.tabs API. Extension runs smoothly, but encounters a problem after chrome.runtime.reload(); occasionally, the chrome.tabs reference becomes undefined upon restart. This renders the extension unusable and ...

The jQuery equivalent for selecting the progress bar value in Webkit would be `$('progress

I'm looking to adjust the transition time for an HTML5 <progress> bar using JavaScript (jQuery), but I've been struggling to find the correct selector in jQuery to achieve this. Here are my current attempts: CSS: progress::-webkit-progre ...

Creating elegant Select Dropdown Box in AngularJS without relying on images

I am currently working on an angular page and have implemented a dropdown with ng-Options to fetch and set values successfully. However, I am now looking to enhance the appearance of this dropdown. After exploring options like ui-select, I realized that be ...