Using Vue.js to import images from a source

Currently, I am in the process of developing an application that features a control panel. One of my objectives is to implement dynamic image changes within the app. However, I have encountered a persistent issue hindering this functionality.

The specific DOM element that I am attempting to modify is as follows:

<div class="bg-image bg-parallax overlay" :style="`background-image:url(${bg1url})`"></div>

This excerpt pertains to the script section found in the Home.vue file:

<script>
import axios from 'axios';
export default {
    name: 'Home',
    data () {
        return{
            page_data: {},
            bg1url: null,
        };
    },
    created() {
        axios.get("http://localhost:5001/api/v1/pages")
            .then((result) => {this.page_data = result.data.page_data});
        this.bg1url = require('@/assets/img/' + this.page_data.background1);
        alert(page_data.background1);
    },
};
</script>

Despite exploring multiple recommendations on platforms like Stack Overflow, none have resolved the issue at hand. It should be noted that I am utilizing the default webpack configurations and project structure.

Please be advised that the portion involving axios fetching from the backend is operational. The primary challenge revolves around integrating the image into the style attribute.

Answer №1

Perhaps the issue lies in setting the value for bg1url outside of the promise (callback function of axios), causing the code to run synchronously instead of asynchronously.

To address this, consider updating your code as follows:

created() {
    axios.get("http://localhost:5001/api/v1/pages").then(result => {
        this.page_data = result.data.page_data
        this.bg1url = require('@/assets/img/' + this.page_data.background1);
    });
},

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

Error message "Render function must return a valid value" occurs when utilizing buttons in mapped details

I am currently working on a project where I fetch data from the backend and display it on the frontend by mapping it out. Everything is functioning well, but when I try to add a button with some functionality (along with binding it and creating a handler) ...

How to Align a Button in the Middle with Bootstrap?

Hey there! I'm working on creating a video list. I've utilized the bootstrap grid system to put it together. Here's the code I have so far: I'm trying to center the play button both vertically and horizontally within the thumbnail. Any ...

Dropzone is encountering an issue with uploading photos: The upload path seems to be invalid

Despite thoroughly checking the path settings in my code, the error message persists when I attempt to upload photos using Dropzone. It seems to be indicating a problem with the upload path. What could be causing this issue? I am currently in the process ...

In ReactJS, the process of rendering a functional component differs from that of a class component

I have a class component that looks like this: import { Component } from 'react'; import { DEFAULT_HPP, DEFAULT_PAGE, DEFAULT_QUERY, PARAM_HPP, PARAM_PAGE, PARAM_SEARCH, PATH_BASE, PATH_SEARCH, } from '../../constants'; ...

Is there a way to automatically have all options pre-selected in a multi-select dropdown menu?

I am trying to populate a multi-select dropdown using data from a JSON object. Here is the code snippet for my select dropdown: <select size="2" id="inst_all" style="width:200px;" multiple ng-multiple="true" ng-m ...

Implementing Voting Functionality with Ajax Submission

Can anyone help me with getting ajax functionality to work properly for my Acts_As_Votable buttons? Here's the current code I have: post_controller.rb def favorite @post = Post.find(params[:id]) @post.upvote_from current_user respond_to do |f ...

The drag-and-drop feature for uploading files is not functioning properly

After following the JavaScript drag and drop script tutorial mentioned here and using the PHP upload script provided here, I made modifications to the XHR request to upload.php in the JS code. The progress now reaches 100%, but the uploaded image does not ...

I'm looking for ways to decrease the size of my Expo build for iOS, which currently sits at approximately 188

After executing the expo build: ios command, I noticed that the generated IPA file is much larger than expected at around 188MB. This seems unusual considering the app has only a few pages with listViews. Any ideas on why the size is so large for iOS whe ...

Guide to showing a preview of an image prior to submitting a form using vue.js

I created a Vue form but I'm having trouble making the image preview function work when users try to submit an image to the form. After refreshing the page, I can see the image, but when uploading it, the preview link is broken. There is an option to ...

The resolveMX function in Google Cloud Functions is encountering issues when trying to process a list of domains

Here is the task at hand. I have a large list of domains, over 100,000 in total, and I need to iterate through them using a foreach loop to resolve MX records for each domain. Once resolved, I then save the MX records into another database. Below is the c ...

Updating specific data in MongoDB arrays: A step-by-step guide

{ "_id":{"$oid":"5f5287db8c4dbe22383eca58"}, "__v":0, "createdAt":{"$date":"2020-09-12T11:35:45.965Z"}, "data":["Buy RAM","Money buys freedom"], & ...

Take the .vue files stored in the components directory and convert them into a JSON format for exporting

I am attempting to scan all .vue files within the components directory and generate a .json file in the root directory. Although my vue.config.ts file is set up as shown below, the custom method I created does not seem to be executing. function createCompo ...

Selecting multiple options in a dropdown menu in React Bootstrap

I'm currently working on incorporating a multi-select feature (similar to select all and deselect all) in a React Bootstrap dropdown menu. <Dropdown> <Dropdown.Toggle variant="success" id="dropdown-basic"> Dropdow ...

Understanding the syntax of a JSON array in jQuery or JavaScript

Hi there, I'm trying to understand this array: { "1":{ "name":"41.52862795108241,-5.397956371307373", "location":"41.52862795108241,-5.397956371307373", "banner":"http:\/\/wpwebsiteinaweekend.com\/wp-content&bs ...

Modify the code for mongodb's insert() function so that it will now only insert data

After experimenting with mongodb for a few days, I've encountered a problem that I'm struggling to explain concisely. The issue arises when my mongodb Collection creates an autoindex, which I want it to do. However, when I insert JSON data like ...

Utilizing a variable within an Angular filter: A step-by-step guide

Currently, I am in the process of experimenting with Angular. I am able to retrieve some data from the controller and successfully apply a filter when passing it as a string. However, I encounter issues when attempting to use a variable for the filter inst ...

How do you properly end a mongoDB connection?

Here is a snippet of the code I've been working on. In the findCollection function, I am retrieving a database service and returning the collection. This function is used in other places like the Base class, which is then utilized elsewhere. My main c ...

What is the best way to utilize the object within the map function?

I have a situation where I am using a typescript code repeatedly and I want to find a way to make it more efficient by reusing the code through assigning it to a variable. An example of this is defining an arrow function within the map function to return a ...

Building a custom Vuetify extension to enhance core features

I am currently working on developing a plugin-based component library to ensure consistency across various product lines using vuetify. However, when I try to install the library and add the components, I encounter multiple errors related to the dark theme ...

Codepen is experiencing difficulties loading textures in three.js

Recently, I've delved into learning three.js using codepen.io. However, I encountered an issue while attempting to add a texture to my box geometry - the texture just won't show up. Can anyone help me troubleshoot this problem in my code? const c ...