You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, scoped CSS is done by compiler.
However, today's CSS has great @scope rule.
By using this, I think we can achieve CSS scoping even more simply.
Example 1
Currently, if include <style> inside <template> it will be ignored, but if only @scope exists it will be passed through.
Example 2
Add .css or .style to component object.
Write your CSS rules inside it.
It's debatable whether to explicitly state @scope or add it internally.
Then insert contents of .css/.style directly under template root.
// When to specifyexportconstMyComponent=defineComponent({template: `<div class="foo">Scoped</div>`,setup(){/*...*/},style: ` @scope { .foo { background-color: cyan; } } `});// When automatically assigned internallyexportconstMyComponent=defineComponent({template: `<div class="foo">Scoped</div>`,setup(){/*...*/},style: ` .foo { background-color: cyan; } `});
I don't know details of how template engine works internally, but I think it goes something like this.
// When to specifyconststyleMergedTemplate=`<style>${component.style}</style>${component.template}`;// When automatically assigned internallyconststyleMergedTemplate=`<style>@scope{${component.style}}</style>${component.template}`;
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Currently, scoped CSS is done by compiler.
However, today's CSS has great @scope rule.
By using this, I think we can achieve CSS scoping even more simply.
Example 1
Currently, if include
<style>
inside<template>
it will be ignored, but if only@scope
exists it will be passed through.<!-- Good --> <template> <style> @scope { .foo { background-color: cyan; } } </style> <div class="foo">Scoped</div> </template> <!-- Bad, ignored --> <template> <style> .foo { background-color: cyan; } </style> <div class="foo">Scoped</div> </template>
Example 2
Add
.css
or.style
to component object.Write your CSS rules inside it.
It's debatable whether to explicitly state
@scope
or add it internally.Then insert contents of
.css/.style
directly under template root.I don't know details of how template engine works internally, but I think it goes something like this.
Both will eventually be inserted into template.
Note
According to Can I Use FireFox is only modern browser that doesn't support
@scope
, but they are currently working on adding feature.Beta Was this translation helpful? Give feedback.
All reactions