gpt4 book ai didi

css - Vue - 在CSS中获取数据属性值

转载 作者:太空宇宙 更新时间:2023-11-04 06:26:11 24 4
gpt4 key购买 nike

在 Vue css 中是否有获取数据属性值的选项?

<template>
<p data-background="purple"> TEST </p>
</template>

<style lang="scss">
p {
background: attr(data-background); //error
&:after {
background: attr(data-background); //error
}
}
</style>

最佳答案

你可以使用 CSS variables对于这种情况。

new Vue({
el: '#app',
data: {
elStyle: {
'--background': 'lightblue',
}
}
});
p:after {
content: 'A pseudo element';
background: var(--background, red); // Red is the fallback value
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<p :style="elStyle"></p>
</div>

关于css - Vue - 在CSS中获取数据属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55071287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com