How to override scoped styles in Vue components?

前端 未结 2 1725
一整个雨季
一整个雨季 2021-02-05 15:17

Let\'s save I have a .Vue component that I grab off of Github somewhere. Let\'s call it CompB and we\'ll add a single CSS rule-set there for a blue header:

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-05 16:02

    After playing around a bit, I think I've got a good approach.

    1. Global overrides

      For situations where I want to override the rules in all cases for CompB I can add a rule-set with more specificity like this: #app .compb-header { .. }. Since we always only have one root (#app) it's safe to use an ID in the rule-set. This easily overrides the scoped rules in CompB.

    2. Parent overrides

      For situations where I want to override both the global and scoped rules. Thankfully VueJS allows adding both a scoped and unscoped style block into a .Vue file. So I can add an even more specific CSS rule-set. Also notice I can pass a class into CompB's props since Vue provides built-in class and style props for all components:

      // in CompA (the parent)
      
      
      
      
      
      
      
      
      

      (note: You can get more specific and make the class you pass into CompB have a more unique name such as .compa-compb or some hash suffix so there is no potential for collision with other components that use CompB and a class .compb. But I think that might be overkill for most applications.)

    3. And of course CompB may provide its own additional props for adjusting CSS or passing classes/styles into areas of the component. But this may not always be the case when you use a component you don't own (unless you fork it). Plus, even with this provided, there is always those situations where you are like "I just want to adjust that padding just a bit!". The above two solutions seem to work great for this.

提交回复
热议问题