脚本宝典收集整理的这篇文章主要介绍了vue 组件中添加样式不生效,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
在开发项目中遇到在组件中添加样式不生效的情况。具体场景如下
//// vue 组件
<template>
<div class="box" data-v-33f8ed40></div>
<template>
//我用js在上面div标签中插入一个<p class='text'>text goes here</p>
<script>
export default {
...
mounted(){
$('.box').html('<p class="text">text goes here</p>')
},
...
}
</script>
//style , vue组件scoped样式都会在选择器的最后加上data-v-***属性
<style scoped>
//样式添加了scoped
.box{
color:red;
}
.text{
color:blue;
}
</style>
浏览器渲染的html 和 style 如下:
//html
<div class="box" data-v-33f8ed40>
<p class='text'>text goes here</p>
</div>
//style
.box[data-v-33f8ed40]{
color:red;
}
.text[data-v-33f8ed40]{ //样式不生效,因为p标签里没有属性data-v-33f8ed40
color:blue;
}
很简单将去掉 style 的 scoped 属性。
以上是脚本宝典为你收集整理的vue 组件中添加样式不生效全部内容,希望文章能够帮你解决vue 组件中添加样式不生效所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。