如何产生
在开发项目中遇到在组件中添加样式不生效的情况。具体场景如下
//// vue 组件
<template>
<div class="box" data-v-33f8ed40></div>
<template>
//我用js在上面div标签中插入一个<p class='text'>text goes here</p>
< >
export default {
...
mounted(){
$('.box').html('<p class="text">text goes here</p>')
},
...
}
</ >
//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 属性。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
-
《法制在线》网络安全,不容小视
2026-05-15栏目: 教程
-
云服务器可以租用吗,价位大概定在多少
2026-05-15栏目: 教程
-
体验未来视界 VR电影酷炫来袭——2019南昌未来影像艺术周暨南昌国际VR电影展开幕侧记
2026-05-15栏目: 教程
-
玩转office只需要这一招!
2026-05-15栏目: 教程
-
1亿人都在用的电商爆发危机
2026-05-15栏目: 教程
