-
Notifications
You must be signed in to change notification settings - Fork 913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parent component could not override style of children. #521
Comments
@JounQin There was a similar request using So, I don't recommend this, but the simple answer for a quick fix would be to add a data-attr to the child component element inside the parent: <!-- parent.vue -->
<template>
<child :class="$style.parent" data-style-from-parent><!-- <<== Added custom data-attr here -->
</child>
</template>
<style module>
.parent[data-style-from-parent] { /* styles here will override child using CSS specificity */ }
</style> |
However, I don't think we need to bloat to Vue or Vue loader to accomplish what you want here. Problems with manually enforcing style render order on dynamic nesting-order-agnostic objects:
To avoid bloating Vue or your project, we can use CSS patterns, and update the docs to be more helpful:
|
This is quite an ugly problem. Anytime you add a class to a child component, whether it's on the root of child component or passed in as a prop and used deeper in the child component, you lose component style encapsulation. Let me demonstrate, vue-loader determines a component tree's style order output as follows: Assume your render tree is <Root>
<A>
<C/>
</A>
<B>
<C/>
</B>
</Root> Assuming each component had styles your css classes output order would be: Root The css classes for C come after A because that was first place C was encountered. So A has to make more specific CSS rules to override C classes. However B does not because it comes after. Now let's say for some reason you updated your render tree to be <Root>
<A/>
<B>
<C/>
</B>
</Root> Now your css classes order would be Root Notice C css classes now appear after B classes. This means simply removing a component in one part of the render tree can switch the css classes output order and potentially break style overrides or lack thereof. |
It seems related with #808 I am just trying to solve it. |
@Jinjiang Great. Can you explain how your PR will work differently for the example I provided above? |
The solution was upgrading vue-loader. vuejs/vue-loader#521
The solution was upgrading vue-loader. vuejs/vue-loader#521
Copied from vuejs/vue#4381 (comment)
Consider two component:
Obviously I want to override the style of
.base
, but actually it does not work because the style ofBaseComp.vue
is loaded afterHello.vue
……http://test.1stg.me/
The text was updated successfully, but these errors were encountered: