I’m new in VueJS and I get confused to change background image from Vue props value.
I’ve created simple table from ‘vue3-easy-data-table’.
BaseTable.vue:
<template>
<EasyDataTable>
...
</EasyDataTable>
</template>
<script setup lang="ts">
changeImg: {
type: String,
}
})
</script>
<style>
.vue3-easy-data-table__message {
background-image: url("`${v-bind("changeImg")}`");
/* background-image: var(--image-url); */
/* background-image: url('@/assets/img/noDataMultiplierOnCity.svg'); */
}
</style>
View.vue:
<template>
<BaseTable
:changeImg= "image"
/>
</template>
<script lang="ts" setup>
const image : string = "'@/assets/img/noDataMultiplierOnCity.svg'"
</script>
I’ve tried solution from this link https://stackoverflow.com/questions/42872002/in-vue-js-component-how-to-use-props-in-css but no gain.
Already tried as in the comments in the code, in this case I can just style the component in style tag cause the class is from ‘vue3-easy-data-table’ (maybe have another way to apply style to it?)
I want to make the background image from BaseTable
so it can be reused in other file.
2
Answers
I hope I understood you right and this example will help you
template:
script:
One way to solve this is to use an inline reactive style. For example you could give your script a method that convers the prop into a style, one that holds the image and any other defining features:
code:
App.vue
BaseTable.vue
Solution using the prop in the CSS
Another way to do this can be to avoid inline styles and instead display the background image in the
<style>
CSS code. To do this, I would use a computed property to create a URL from the prop, something like:Code example,
BaseTable2.vue
Both examples can be found at the Vue SFC Playground