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

问题描述 投票:0回答:1

有什么选项可以在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 vue.js sass styles custom-data-attribute
1个回答
1
投票

你可以在这种情况下使用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>
© www.soinside.com 2019 - 2024. All rights reserved.