[英]how to add Vue props value to background-image
I'm new in VueJS and I get confused to change background image from Vue props value.我是 VueJS 的新手,我对从 Vue 道具值更改背景图片感到困惑。
I've created simple table from 'vue3-easy-data.table'.我从“vue3-easy-data.table”创建了简单的表格。
BaseTable.vue:基表.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:查看.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.我试过这个链接https://stackoverflow.com/questions/42872002/in-vue.js-component-how-to-use-props-in-css的解决方案,但没有收获。
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?)已经按照代码中的注释进行了尝试,在这种情况下,我可以只在样式标签中设置组件样式,因为 class 来自“vue3-easy-data.table”(也许有另一种方法可以对其应用样式?)
I want to make the background image from BaseTable
so it can be reused in other file.我想从
BaseTable
制作背景图像,以便它可以在其他文件中重复使用。
I hope I understood you right and this example will help you我希望我理解正确,这个例子会对你有所帮助
template:模板:
<div :style="styleExample" />
script:脚本:
let styleExample = { 'width': props.examplePro }
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:
例如,您可以为您的脚本提供一种将道具转换为一种样式的方法,该样式包含图像和任何其他定义特征:
<template>
<EasyDataTable :style="backgroundStyles(image)">
...
</EasyDataTable>
</template>
<script setup>
changeImg: {
type: String,
}
})
const backgroundStyles = (img) => {
return {
'background-image': `url(${img})`,
'background-size': 'cover'
}
}
</script>
code:代码:
<script setup>
import { ref } from 'vue'
import BaseTable from './BaseTable.vue'
import BaseTable2 from './BaseTable2.vue'
const msg = ref('Hello World!')
const imageUrl = ref("https://cdn.mos.cms.futurecdn.net/SWx64q2g3wax53Xz5H4QjS-970-80.jpg.webp");
</script>
<template>
<h1>{{ msg }}</h1>
<input v-model="msg">
<BaseTable :image="imageUrl"/>
<hr>
<BaseTable2 :image="imageUrl"/>
</template>
<template>
<div class="bkgrnd" :style="backgroundStyles(image)">
<h2>
Base Table
</h2>
<ul v-for="index in 8" :key="index">
<li>Index: {{ index }}</li>
</ul>
</div>
</template>
<script setup>
const props = defineProps(['image'])
const backgroundStyles = (img) => {
return {
'background-image': `url(${img})`,
'background-size': 'cover'
}
}
</script>
<style scoped>
.bkgrnd {
color: white;
font-style: bold;
}
</style>
Another way to do this can be to avoid inline styles and instead display the background image in the <style>
CSS code.另一种方法是避免内联 styles,而是在
<style>
CSS 代码中显示背景图像。 To do this, I would use a computed property to create a URL from the prop, something like:为此,我将使用计算属性从 prop 创建一个 URL,类似于:
const computedUrl = computed(() => {
return `url(${props.image})`;
});
Code example,代码示例,
<template>
<div class="bkgrnd">
<h2>
Base Table 2
</h2>
<ul v-for="index in 8" :key="index">
<li>Index: {{ index }}</li>
</ul>
</div>
</template>
<script setup>
import { computed } from 'vue';
const props = defineProps(['image'])
const computedUrl = computed(() => {
return `url(${props.image})`;
});
</script>
<style scoped>
.bkgrnd {
color: white;
font-style: bold;
background-image: v-bind(computedUrl);
}
</style>
Both examples can be found at the Vue SFC Playground这两个示例都可以在Vue SFC 游乐场找到
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.