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

标签 css vue.js sass styles custom-data-attribute

在 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/

相关文章:

css - 在 Windows 上使用 PrePros 编译 Sass

css - Bootstrap 文本字段突然缩小

html - 调整窗口大小时保持元素与背景的相对位置?

html - 无法将面包屑项移动到右侧

javascript - 如何使用 Vue2 一起管理 v-bind 和 v-on?

node.js - 无法加载 gulp : Error: ENOENT: no such file or directory 'node-sass/vendor'

configuration - sass/scss/compass : how to set default project file structure

javascript - 切换显示 :none style with JavaScript

jquery - 阻止 div 动画在未来被触发

css - 为什么 SVG textPath 不渲染? [开发工具中的 0x0]