2

I've a third party component that I'm extending - while I want to reuse template and style section, I need to alter the behaviour of it. So, the content of my vue file looks something like this:

<script>
import Foo from 'foo';
export default {
  name: 'Bar',
  extends: Foo
}
</script>

Unfortunately, while template section of Foo is reused in my Bar the same doesn't happen for the style section - I've to copy it in its entirety from Foo otherwise no styles will be applied.

How can I reuse parents style section?

4
  • 1
    There doesn't seem to be a solution to this now. But there is already a feature request in github.com/vuejs/vue-loader/issues/1003. Commented Aug 3, 2018 at 9:44
  • Could you not use an imported file rather than an online style? Commented Aug 3, 2018 at 10:35
  • @Marcus - could you please explain? Commented Aug 3, 2018 at 11:16
  • Hi there, please see answer below. Commented Aug 3, 2018 at 11:31

1 Answer 1

2

When you define a Vue component, you have an optional section, where you can either place inline CSS or import an external file. By default, when creating a component (for the sake of argument call it any-component.vue) I will include the following section:

<style scoped>
    @import ‘./any-component.css’
</style>

The scoped parameter prevents the CSS propagating elsewhere in the DOM, so the styling is only applied to the component itself and does not corrupt other components. But depending on your needs you can remove it.

So this gives you two solutions: there is nothing to stop you either importing a CSS file into the parent without the scoped qualifier; lor, importing the same CSS file into multiple components (when you want a consistent look and feel). With a view to avoiding unintended consequences, I would opt for the latter.

Hope that helps.

Sign up to request clarification or add additional context in comments.

3 Comments

In my instance parent component has an inline style section and it doesn't import an external css file.
Unfortunately this solution is contrary to the single file component methodology of Vue.
I'm not sure Vue has a particularly strong view on the subject. An app scaffolded with Vue-cli comes prepackaged with the ability to handle CSS modules. And in any event it all depends on the situation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.