7

I'm building a banner with Vue that needs to have a dynamic background, however, it doesn't seem to be working. Not sure what I'm doing wrong. I've tried a few other ways and it works if I do an image tag something like

<img :src="require(`@/assets/images/${backgroundImage}`)" />

But obviously this needs to be an inline background image.

Code:

component

<template>
  <div
    class="w-full h-64 bg-auto bg-no-repeat bg-center lg:bg-cover relative"
    :style="{ backgroundImage: url(require('@/assets/images/' + backgroundImage))}"
  >
    <div class="w-full h-full flex flex-col justify-center items-center text-white px-6">
      <div class="hero-text rounded text-center py-8 px-12">
        <p class="text-base lg:text-md uppercase font-medium">{{ smallLeadingText }}</p>
        <h1 class="text-xl md:text-3xl lg:text-5xl uppercase font-bold">{{ mainText }}</h1>
        <p class="text-base lg:text-md">{{ subText }}</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "PageHero",
  props: {
    backgroundImage: String,
    smallLeadingText: {
      type: String,
      required: false
    },
    mainText: {
      type: String,
      required: true
    },
    subText: {
      type: String,
      required: false
    }
  }
};
</script>

View

<PageHero
  backgroundImage="mc-background.png "
  smallLeadingText="Powerful, secure &amp; affordable"
  mainText="Minecraft hosting"
  subText="Plans suitable for all budgets"
/>
4
  • Would you be able to give an example @Phil? If you don't mind Commented Mar 26, 2020 at 1:16
  • So I tried your code @Phil but it removes the code gyazo.com/09f006c0bdef02156051924a9f30037b if I remove the :style the code shows without the background Commented Mar 26, 2020 at 1:18
  • I may have messed something up in the comment so I've moved it to an answer below. I'd definitely preference using computed properties instead of doing everything inline Commented Mar 26, 2020 at 1:22
  • FYI, I've always found it works best to use kebab-cased attribute names when passing props, ie background-image="mc-background.png". See vuejs.org/v2/guide/… and vuejs.org/v2/style-guide/#Prop-name-casing-strongly-recommended Commented Mar 26, 2020 at 1:56

2 Answers 2

9

Looks like you've just got some syntax errors in your style attribute around string quoting. Try

<div :style="{ backgroundImage: `url(${require('@/assets/images/' + backgroundImage)})` }">

Might be easier to create some computed properties to resolve everything though

computed: {
  bgImage () {
    return require('@/assets/images/' + this.backgroundImage)
  },
  inlineStyle () {
    return {
      backgroundImage: `url(${this.bgImage})` 
    }
  }
}

and

<div :style="inlineStyle">

Demo ~ https://codesandbox.io/s/crimson-sky-ehn9r

Sign up to request clarification or add additional context in comments.

6 Comments

So I tried your code but it removes the code gyazo.com/09f006c0bdef02156051924a9f30037b if I remove the :style the code shows without the background even with the computed route.
Same issue for both examples just shows the component as commented <---> and doesn't actually show with the style bind.
@debugabug works fine for me. I've added a demo link
Hey, here is a small video of what is happening, not sure why as everything is correct given the example link. i.gyazo.com/91d584adb32ef0f148e1a13397b3c1d6.mp4
@debugabug There's three errors in your browser console. What are they?
|
0

Point 1: Syntax

While the other solution does not work for me, this does:

<div :style="{ backgroundImage: `url(${'src/assets/images/' + backgroundImage})`}"></div>

Of course, you need the height property to display the image.

This solution does not include "require", which will save you the error of "require" not defined.

Point 2: Path

You can go to the browser's console, Network tab, and check if the image actually gets loaded. The path may be incorrect, for example:

'../assets/images/'

and

'@/assets/images/' 

did not work for me, but this:

'src/assets/images' 

and this:

src/images

did, depending on the project. I have not looked into why 'assets' was skipped for some projects and not others, but for now you can try these options.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.