0

For a project where Vue is dropped in, is using style or similar available to components?

Vue.component('vue-sup', {
    template: '<div>Sup</div>',
    style: '* { color: blue; }'
})    

I tried adding the styles inside the template like:

<div>
    <style> 
        .here{} 
    </style>
    <div>Sup</div>
</div> 

which didn't work because the template parser detected a tag with side effects

2 Answers 2

1

Vue's implementation of scoped css is entirely a feature of vue-loader, and thus only works with compilation. Scoped css momentarily made a debut into Html 5 but saw almost no adoption and was dropped entirely as far as I know. There is the anticipation that "Shadow DOM" may be supported broadly and could be use to add scoped css, but adoption is not there yet either.

So at this point you can add unique classes or ids obviously to a parent container and scope your css that way, but is understandably not what you are asking for nor is it always practical.

The best alternative is a pollyfill. There are several that are available. Here is one by Sam Thorogood and another by Thomas Park but if you do a quick search you will likely discover more.

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

3 Comments

Really excellent solution. Only problem is, when I include a <script> tag inside of the template, I get an alert stating "Avoid placing tags with side-effects in your templates, such as <style>, as they will not be parsed."
It's not really a valid solution, but I edited Vue's source to not look at <style> tags. Is there a way I can turn that into a plugin? Function in question is isForbiddenTag(el)
Not sure, but did you try calling the function Vue.config.ignoredElements after your app.js (vue.js) file? vuejs.org/v2/api/#ignoredElements
0

I came across the same problem and I'm able to insert styling inside Vue template by creating a component that will dynamically insert a <style> tag on the DOM. This might be impractical like @skribe said but it allows me to have separate CSS from JS without using .vue extension.

You can take a look at this

Comments

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.