Link an image from an outside resource

I'm trying to link a background image from an external source using Vue.js. This is the code snippet:

<div class="image-box border"
            :style="{ background: `url(`+ item.image +`) no-repeat center` }" 
            style="width: 220px; height: 220px">

I also attempted this method:

<div class="image-box border" 
            :style="{ 'backgroundImage': 'url(' + item.image + ')' }">

Here is how the URL appears in the app with no errors:

background-image: url("http://localhost:8080/assets/images/shoe-1.png");

Any assistance would be greatly appreciated!

Full code:

component -

<template>
    <div class="product-card-box border">
        <div class="image-box border"
                :style="{ background: `url(`+ item.image +`) no-repeat center` }" 
                style="width: 220px; height: 220px">

        </div>
        <div class="info-box border">
            <div class="color-info product-info bold">{{item.colors.length}} color</div>
            <div class="product-name">
                <div class="product-info bold">{{item.name}}</div>
                <div class="product-info sub-info">{{item.gender}}'s Shoe</div>
            </div>
            <div class="product-price">
                <div class="product-info sub-info">${{item.price}}</div>
            </div>

        </div>
    </div>
</template>

data -

const data = [
{
    name: 'SNKR 001',
    gender: 'Men',
    price: 100,
    sport: 'running',
    width: 'Wide',
    colors: ['black', 'white', 'green', 'pink'],
    sizes: [3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, 13, 14, 15],
    image: '../assets/images/shoe-1.png'
},
{
    name: 'SNKR 002',
    gender: 'Men',
    price: 100,
    sport: 'running',
    width: 'Wide',
    colors: ['black', 'white', 'green', 'pink'],
    sizes: [3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, 13, 14, 15],
    image: '../assets/images/shoe-1.png'
}
];
export default data;

Answer №1

When working with data[], make sure to utilize require(path) in order to accurately resolve the path within your template:

photo: require('../assets/images/shoe-1.png')

Answer №2

Instead of using multiple style objects, consider simplifying to just one. Do you really need a background image? You might be able to achieve the same effect using an img tag with a class and applying object-fit.

<div id="app">
  <div :style="{ 
    width: '200px',
    height: '200px',
    'background-image': `url(${backgroundImage}`,
   }"/>
</div>

new Vue({
  el: "#app",
  data: {
    backgroundImage: 'http://picsum.photos/g/1200/400?image=30',
  },
})

Take a look at this fiddle showcasing the object-fit solution https://jsfiddle.net/caseyjoneal/b5r8fvjq/73/

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

Ways to create an AngularJS directive for dynamically allocating scope bindings

Can a directive be used to define the scope bindings of an element? For example: <div g:bind="{width: '=', height: '@'}" width="myWidth" height={{myHeight}}></div> ...

Troubleshooting PhantomJS hanging with WebdriverJS tests on Windows

I am currently using webdriverjs to run automated tests on a Windows 8 system. The tests run successfully when the browser is set to Chrome, but encounter issues when I switch to PhantomJS. Interestingly, the same tests run smoothly on OS X Mavericks. Ins ...

How can I determine in JavaScript if the Backspace key is being long pressed on iOS or Android devices?

In the process of developing a web application using Vue.js, I encountered an issue with detecting long presses of the Backspace key on desktop keyboards (Windows PCs specifically). On desktop, the event triggers multiple times as long as the Backspace key ...

Enhancing JSON data in Datatables with additional text

I'm currently looking for a way to insert some text into my data before generating a table using jQuery DataTables. As an example, if my JSON data looks like [1,5,6,12], I would like it to be displayed as [1 seconds, 5 seconds, 6 seconds, 12 seconds] ...

How to retrieve properties of the final item in an array using Vue.js

Struggling with Vue.js JavaScript implementation. This is the current code: <div id="app"> <h1>Items</h1> <div v-for="item in items"> <input v-model="item.val"> </div> <button @click="addItem"> Ne ...

Combining Vueify with Elixir and Hotloading led to an unexpected error: Uncaught TypeError - property 'indexOf' is undefined

I have successfully configured Elixir to utilize Vueify with a hot reload plugin. The compilation process runs smoothly, but I encounter a console error in my compiled file and the Vue component does not seem to transform into HTML; it still displays the & ...

Vue.js in conjunction with webpack fails to provide compressed GZIP .js files

I am facing an issue with serving GZIPd JavaScript files from my server to the client. My Simple Vue.js application is hosted on Heroku. After building the site using "npm run build" in the console, I noticed that the /dist/js directory contains 4 differe ...

The jQuery selector is failing to respond

Attempting to create a slow downward movement effect on text as I scroll down the page, but encountering an issue with my jQuery selector not firing properly. Sample HTML: <div class="stairsImage"> <h1 id="testName">Lorem Ipsum</h1> ...

What is the best way to provide two unique versions of websites using one domain?

I've embarked on a project that requires a complete rewrite. Instead of opting for a big bang release, we've decided to utilize the Strangler Pattern as outlined here. The current application (details below) will continue to run unchanged under ...

Achieving uniform alignment of multiple field labels in Bootstrap and Simple Form

Here is the current layout of my form: https://i.sstatic.net/2vZjl.jpg You may notice that the labels are not properly aligned. I would like them to appear like this: https://i.sstatic.net/gm39D.jpg Below is the code for all the input fields: <%= si ...

Add a fading transition feature to images that are loaded at a later time

I used a clever technique to create a blur effect by loading a small, lightweight image first. Once the main background image is loaded, it swaps out the 'data-src' with the actual image. However, I am facing an issue with the abrupt transition, ...

I am looking to modify the appearance of specific sections of a searched word in React.js, such as making them bold or lighter. Can anyone

After coming across several similar questions, I realized none quite matched my issue. Specifically, I am attempting to style only the part of the search result that relates to the entered query. For instance, if the query is "Thi", I want the result to di ...

Modifying the weight of fonts in TVML components

Currently, I'm in the process of developing a TVML app specifically for the Apple TV. Lately, I've been experimenting with making some style adjustments to various elements within the app. Following the guidance provided in the TVML documentatio ...

How can I adjust two overlapping divs for proper display?

It seems like I have two divs overlapping on the same line. The layout appears fine on a PC but not on an iPad or mobile device. Can someone please review the code and point out any mistakes I might have made? The issue is with the content overlapping the ...

What is the process of retrieving JSX within an object using a function that searches for data (in the form of JSX) in a separate

I am trying to extract JSX content from another file called categoriesDetails and store it in an object (foundCategory) by using the find function to check the item.title. Below is my implementation: This is how I am retrieving the data and intending to p ...

Issue with using float-right in Bootstrap v4. [Alternative to pull-right]

I am currently using the most recent version of Bootstrap, which is v4.0.0-beta.3. I am having trouble getting elements to align properly according to the official documentation. Despite searching online for solutions, I have not been able to find any that ...

Is there a way to convert Excel data into JSON format and then automatically write it to a JSON file using Cypress?

After attempting to convert an excel file into json format using my code, I encountered some difficulties in obtaining the desired output. Instead of a proper conversion, it seems to return an array of each row. Any assistance in reading the data correctly ...

Trigger an email notification in Google Sheets whenever a designated cell is populated

I am currently exploring ways to enhance collaboration in project management, especially when dealing with numerous stakeholders (Although there are free tools available, integrating new procedures into a public organization's projects can be quite ch ...

Service for Posting in Angular

I am looking to enhance my HTTP POST request by using a service that can access data from my PHP API. One challenge I am facing is figuring out how to incorporate user input data into the services' functionality. Take a look at the following code snip ...

Utilizing JSON compression techniques on both the client and server ends for more efficient data transfer

I'm searching for a tool that can compress JSON on the server side (using C#) and then decompress it on the client side, as well as vice versa. The entire data model for my webpage is in JSON format and I need to find a way to reduce its size. I' ...