如何產生
在開發項目中遇到在組件中添加樣式不生效的情況。具體場景如下
//// 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-33f8ed40color:blue;}如何解決
很簡單將去掉 style 的 scoped 屬性。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持錯新站長站。
新聞熱點
疑難解答
圖片精選