Tips for linking img src attribute with a dynamic url in Vue.js

I'm currently working on a Vue project where I am attempting to bind the img src attribute to a data value. I am using a setInterval function to dynamically change the URL every time the function is called. However, when I run the page, I encounter an error message:

Property or method "source" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

found in

My question is how can I achieve this kind of binding and observe the image changes when the related function is invoked by the setInterval function.

Here's my code snippet:

<template>
  <v-card elevation="5" class="mx-auto mt-2" shaped max-width="800">
    <v-img
      class="white--text align-end"
      gradient="to bottom left, rgba(0,0,0,0.2), rgba(0,0,0,0.2), rgba(255,0,0,0.4), rgba(0,0,0,0.8)"
      height="500px"
      aspect-ratio="1.7"
      :src="this.source"
    >
      <v-card-title class="overline">
        <span class="red--text darken-4">
          LIVE
          <v-icon class="red--text darken-4 ma-2" size="3">mdi-circle</v-icon>
        </span>SULTANBEYLI, ISTANBUL | 06/13/2020 - 13.35 PM
      </v-card-title>
    </v-img>
  </v-card>
</template>
<script>
export default {
  name: "Camera",
  sockets: {
    frames(data) {
      console.log(data);
    }
  },
  methods: {
  },
  mounted() {
     setInterval(() => {
        this.ex += 1;
        console.log(this.source)


    }, 60000); 
  },
  data: () => ({
    ex: "1",
  })
};

Answer №1

Here is a simple example to illustrate:

<template>
  <div>
    <img :src="image" />
  </div>
</template>

<script>
export default {
  name: 'App',
  data: () => ({
    image: null,
    imageCode: 1,
  }),
  mounted() {
    setInterval(() => {
      if (this.imageCode == 1) {
        this.image =
          'https://smaller-pictures.appspot.com/images/dreamstime_xxl_65780868_small.jpg';
        this.imageCode = 2;
      } else {
        this.image =
          'https://cdn3.wpbeginner.com/wp-content/uploads/2020/03/ultimate-small-business-resource-coronavirus.png';
        this.imageCode = 1;
      }
    }, 2000);
  },
};
</script>

You might be encountering an issue with the "ex" variable data type in your code. Try using an integer value instead:

  data: () => ({
    ex: 1,
    source: "http://aua.0xseck.fun/tr/1/1.jpg"
  })

Also, when declaring in the template, try omitting "this":

<v-img :src="source"></v-img>

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

Chrome failing to import images, but Firefox has no issues with image importing

I'm not very familiar with JavaScript or jQuery, but I was attempting to troubleshoot a functionality that was previously created. The issue is that it works fine on Firefox, but it fails on Chrome. I am importing an image and then cropping the necess ...

Is there a way to cancel a subscription in Stripe using React?

I'm currently working with React and I need to remove an old subscription when a user changes their plan. Here is the code snippet I am using: import {loadStripe, useStripe} from '@stripe/stripe-js' const loadCheckout = async (priceId) => ...

Trouble encountered while integrating Cloudinary with ExpressJS

I am facing an issue while working on my expressjs code with cloudinary. When I try to set the path for uploading an image, it throws an error saying that the path is not defined. I'm having trouble figuring out what went wrong. This is how I have im ...

Incorporating a way to automatically initiate a GET request following a POST request in React can effortlessly display a new

In my App component, I display a list of items and have a ModalForm where users can add new items. However, after adding a new item, I currently have to reload the page in order to see it in the list. I am looking for a way to automatically trigger a GET ...

JavaScript: Understanding the concept of closure variables

I am currently working on an HTML/JavaScript program that involves running two counters. The issue I am facing is with the reset counter functionality. The 'initCounter' method initializes two counters with a given initial value. Pressing the &a ...

Oddly enough, the toggle hide/show button only activates two lines after the SECOND click, not the first

After the first click, some lines of code work properly while others only function correctly after the second click. Reviewing the comments within the code may provide a clearer understanding. I am seeking assistance in identifying where my logic is flawed ...

Unnoticed modification to an Angular directive's attribute

I'm encountering an issue with an Angular directive. I'm trying to create a directive that can "change itself" based on an attribute. Unfortunately, the watcher on the attribute isn't detecting any changes. Here is a fiddle in JavaScript t ...

Vue - Switching between 2 card tabs causes tinyMCE to vanish

My Vue Bootstrap Card component contains a tinyMCE editor wrapped in <tiny-mce-editor></tiny-mce-editor>. However, when I switch to the second tab and then back, the editor turns into a plain textarea. I have tried numerous solutions found on ...

What is the correct method for integrating jQuery libraries into an Angular project version 10 or higher?

Currently, I am facing difficulties while attempting to install the jquery and jquery-slimscroll packages into an Angular project (version greater than 10). It appears that the installation of these packages has not been successful. In light of this issue, ...

Building a jQuery function to filter JSON data based on user input without relying on any external filter

Is it possible to dynamically filter a list of JSON data based on the value entered in a search input box for a specific key, such as 'firstname'? I am new to JavaScript and jQuery and would appreciate any help. HTML <input type="search" nam ...

Issue with Karma and angular-mocks: The error message "TypeError: 'undefined' is not an object (evaluating 'angular.mock = {}')" is being shown

I am facing an issue while writing unit tests using Karma + Jasmine. The problem arises with angular-mocks when I run grunt test, resulting in the following error message: PhantomJS 1.9.8 (Mac OS X) ERROR TypeError: 'undefined' is not an ob ...

Is there a way to disable the automatic refreshing of the useForm feature?

Upon clicking submit, all field information is supposed to be sent to the backend, but instead, it is being appended to the browser's URL. Furthermore, the error messages from yup are not being displayed. I attempted to use event.preventDefault in ha ...

Repetitive data in a list with AngularJS

There's probably a simple solution to this: I have some data in a controller, and depending on whether an item is selected or not, it should display different HTML. If the item is selected, the HTML should be: <li> <a href="#"><s ...

What is the method for transferring a script-defined variable to a division tag in an ASP.NET MVC5 Razor view?

I am looking to pass the value of the target from a script to a division tag in MVC. Below are the codes I have implemented. Script: @section Scripts{ <script type="text/javascript"> $('a[data-toggle="tab"]').on('show ...

Does NextJs <Link> not fire the beforeunload event? Wanting to warn the user before they navigate away from the page

How do I prompt the user before leaving the page? I have noticed that when the user enters the page from a standard link element, the beforeunload event is triggered when they click back. However, if the user enters the page through the NextJs Link compon ...

Manipulating CSS Class Properties Using JavaScript

In my JavaScript application, there is a functionality that loads a list of items for users to click and view detailed information in a separate div on the page. Users should be able to interact with and make modifications to these individual item overview ...

Ensuring the cookie remains active throughout various subdomains

After implementing code to set a cookie on example.com, I noticed an issue with it carrying over to the subdomain dogs.example.com. <script> document.cookie="cid1={{utm_campaign}}; path=/;" </script> The {{}} is part of Google-Tag-Manager s ...

Discover the best way to retrieve XML information from various sources specifically designed for Windows gadgets using JavaScript

I have previously visited this site and have not been able to locate a solution that I believe will be suitable for my Windows 7 desktop gadget. In essence, I am seeking a method to retrieve XML data from weather.gov using Javascript (or any other means t ...

What steps can be taken to ensure that the application loading process is not reliant on web service

I recently developed a PhoneGap application that initiates a web service call upon loading the index.html page. The call is made using the following code: $.ajax({ type: "GET", url: "http://webserviceurl.com/service", cache: false, async: true, ...

NodeJS API integration failure during ReactJs login attempt

Currently, I am tackling a small project on implementing user login functionality in ReactJs with Nodejs and Express-session. The issue I am facing is that my front end (React login page) isn't functioning properly. Below is the Fetch API code snippet ...