UPDATE: Just a heads up that your method did indeed work. The only thing missing was setting the width and height properties on the div element, causing it not to display on the screen.
See it in action here
If you're looking to dynamically set the backgroundImage using a URL, consider utilizing the following strategy:
[Options API]
<template>
<div
:style="{
backgroundImage: `url(${imageUrl})`,
width: '100px',
height: '100px',
}"
></div>
</template>
<script>
export default {
name: "App",
data() {
return {
imageUrl:
"Your image url",
};
},
};
</script>
Alternatively, you can opt for the Composition API [script setup]
<script setup>
import { ref } from "vue";
const imageUrl = ref(
"https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/1200px-Image_created_with_a_mobile_phone.png"
);
</script>
<template>
<div
:style="{
backgroundImage: `url(${imageUrl})`,
width: '100px',
height: '100px',
}"
></div>
</template>