带有作用域 css 的 Vue.js 样式 v-html [英] Vue.js style v-html with scoped css
问题描述
如何使用 vue-loader 为 v-html 内容设置范围内的 css 样式?
How can I style v-html content with scoped css using vue-loader?
简单例子:组件.vue
Simple example: component.vue
<template>
<div class="icon" v-html="icon"></icon>
</template>
<script>
export default {
data () {
return {icon: '<svg>...</svg>'}
}
}
</script>
<style scoped>
.icon svg {
fill: red;
}
</style>
生成html<div data-v-9b8ff292="" class="icon"><svg>...</svg></div>
生成css.info svg[data-v-9b8ff292] { 填充:红色;}
如您所见,v-html 内容没有 data-v 属性,但生成的 css 具有用于 svg 的 data-v 属性.
As you can see v-html content don't have data-v attribute, but generate css have data-v attribute for svg.
我知道这是 vue-loader 的预期行为 (https://github.com/vuejs/vue-loader/issues/359).在这个问题中提到了后代选择器.但正如你所看到的,我在我的 css 中使用了它,但它不起作用.
I know this is expected behavior for vue-loader (https://github.com/vuejs/vue-loader/issues/359). And in this issue descendent selectors mentioned. But as you can see I use it in my css and it's not worked.
如何设置 v-html 内容的样式?
How can I style v-html content?
推荐答案
vue-loader
的新版本(从 12.2.0 版开始)允许您使用深度范围"的 css.你需要这样使用它:
New version of vue-loader
(from version 12.2.0) allows you to use "deep scoped" css. You need to use it that way:
现在支持可以影响子级的深度"选择器使用
>>>
组合子的组件:
<style scoped>
now support "deep" selectors that can affect child components using the>>>
combinator:
.foo >>>.bar { 颜色:红色;}
将被编译成:
.foo[data-v-xxxxxxx] .bar { 颜色:红色;}
关于vue-loader
发布页面的更多信息>
More informations on the release page of vue-loader
这篇关于带有作用域 css 的 Vue.js 样式 v-html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!