I'm currently working on a Vue banner that requires a dynamic background, but for some reason, it's not functioning as expected. I've experimented with different methods, and using an image tag like this works:
<img :src="require(`@/assets/images/${backgroundImage}`)" />
However, I need this to be an inline background image.
Here's the 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 & affordable"
mainText="Minecraft hosting"
subText="Plans suitable for all budgets"
/>