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
It allows for proper encapsulation of components. Currently the class and style props cannot be intercepted or prevented from being injected into the root element of a component. This prevents the ability to refactor components without requiring the calling code to be changed.
For example, assume a Logo component which is used with a class attribute:
<my-logo class="app-logo" />
With the template:
<template>
<img src="my-logo.png" />
</template>
Due to the merging of the class attribute the rendered tag will be:
<img class="app-logo" src="my-logo.png" />
Now, let's assume we want to refactor this component so it lazily loads the image. Our new template is:
Now the class attribute is put on the div, which may not be appropriate. With good encapsulation, there would be a way to define class as a prop to intercept the value being passed in and render it on a different element than the root element, such as:
Currently, attempting to define class as a prop displays this warning on the console:
[Vue warn]: "class" is a reserved attribute and cannot be used as component prop.
While inheritAttrs can be set to false to disable most props from being passed through to the root element, it does not apply to class or style.
What does the proposed API look like?
Ideally simply allow class and style as valid Vue props that can be used to intercept the values passed in (that is, they don't pass through to the root element when these are defined as props).
If that is not possible for backward-compatibility reasons, add a flag similar to inheritAttrs that enables this functionality (e.g. enableClassAndStyleProps).
The text was updated successfully, but these errors were encountered:
This would be a breaking change for Vue 2 so it won't happen, but it already works that way on Vue 3 when declaring a prop named class.
I personally don't think it is worth adding yet, another option that goes with inheritAttrs to prevent class and style from falling through, but let's see what other people feedback is
What problem does this feature solve?
It allows for proper encapsulation of components. Currently the
class
andstyle
props cannot be intercepted or prevented from being injected into the root element of a component. This prevents the ability to refactor components without requiring the calling code to be changed.For example, assume a
Logo
component which is used with aclass
attribute:With the template:
Due to the merging of the
class
attribute the rendered tag will be:Now, let's assume we want to refactor this component so it lazily loads the image. Our new template is:
Now the
class
attribute is put on thediv
, which may not be appropriate. With good encapsulation, there would be a way to defineclass
as a prop to intercept the value being passed in and render it on a different element than the root element, such as:Currently, attempting to define
class
as a prop displays this warning on the console:While
inheritAttrs
can be set tofalse
to disable most props from being passed through to the root element, it does not apply toclass
orstyle
.What does the proposed API look like?
Ideally simply allow
class
andstyle
as valid Vue props that can be used to intercept the values passed in (that is, they don't pass through to the root element when these are defined as props).If that is not possible for backward-compatibility reasons, add a flag similar to
inheritAttrs
that enables this functionality (e.g.enableClassAndStyleProps
).The text was updated successfully, but these errors were encountered: