Home  >  Article  >  Web Front-end  >  How to solve the problem that the parent component cannot modify the style of the child component after adding scoped in Vue development

How to solve the problem that the parent component cannot modify the style of the child component after adding scoped in Vue development

零到壹度
零到壹度Original
2018-04-04 15:38:043483browse

In the development of vue, we need to reference sub-components, including ui components (element, iview). But after adding scoped in the parent component, writing the style of the child component in the parent component has no effect. After removing scoped, styles can be overridden. But this will pollute the global style. In order to solve this problem, vue-loader adds a new writing method.

#demo

Parent component:

2e9329280d0752fd10b47f2b219c80f6

 header /deep/ .header{     
   box-shadow:0px 1px 0px 0px $white;
  }
header >>> .header{   
   box-shadow:0px 1px 0px 0px $white;
}
531ac245ce3e4fe3d50054a55f265927

Child component:

d477f9ce7bf77f53fbcf36bec1b69b7a  
 1aa9e5d373740b65a0cc8f0a02150c53    
    615b1814b75ff9e0c69df6c409d99690       
    94b3e26ee717c64999d7867364b1b4a3   
  ab946e7546ab66a280dd9c9f9310ecd5
21c97d3a051048b8e55e3c8f199a54b2

This way of writing and modifying the style of the child component , without polluting the global style!

Official document: Scoped CSS · vue-loader

Tips: This method is supported starting from vue-loader 11.2.0

In development using vue, we sometimes refer to external components, including UI components (ElementUI, iview).

When the c9ccee2e6ea535a969eb3f532ad9fe89 tag has the scoped attribute, its CSS only applies to elements in the current component.
But after adding scoped to the parent component, the style of the parent component will not penetrate into the child component, so writing the style of the child component in the parent component has no effect.

一、去掉 scoped

在父组件的 c9ccee2e6ea535a969eb3f532ad9fe89 中去掉 scoped 后,父组件中可以书写子组件的样式,但是你会担心这样会污染全局样式。 
【因为我们知道正确使用全局样式的姿势是使用一个全局的 app.css】

二、混用本地和全局样式

你可以在一个组件中同时使用有作用域和无作用域的样式:

<style>
/* 全局样式 */
</style>
<style scoped>
/* 本地样式 */
</style>

我们把 需要修改子组件的样式 写在上面那个全局样式里面

三、使用深度作用选择器

如果你希望 scoped 样式中的一个选择器能够作用得“更深”,例如影响子组件,你可以使用 >>>   /deep/ 操作符:

<style scoped>
.a >>> .b {
  /* ... */
}
.a  /deep/  .b { 
 /* ... */
}
</style>

有些像 SASS 之类的预处理器无法正确解析 。这种情况下你可以用 /deep/ 操作符取而代之 —— 这是一个  的别名,同样可以正常工作。


OK,主要内容就是以上几点。

需要额外补充的是: 
1、通过 v-html 创建的 DOM 内容不受作用域内的样式影响,但是你仍然可以通过深度作用选择器来为他们设置样式 
2、CSS 作用域不能代替 class 
3、在递归组件中小心使用后代选择器

The above is the detailed content of How to solve the problem that the parent component cannot modify the style of the child component after adding scoped in Vue development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn