这篇文章将为大家详细讲解有关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;
}
关于vue组件中添加样式不生效如何解决就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。