Pass Classes Explicitly via Props
Learn to avoid the issues in migration while passing the classes or styles to a component.
The $attrs
object now contains all attributes passed to a component, including class and style.
Vue 2
In Vue 2, v-bind=" $attrs"
forwards all properties that were passed to a component, but not classes and styles, because these get special treatment and are applied to the component’s root element:
<!-- InputField.vue --><template><label><input type="text" v-bind="$attrs" /></label></template><script>export default {inheritAttrs: false}</script>
When used like this:
<InputField id="input-id" class="input-field-class"></InputField>
The InputField
component generates this output:
<label class="input-field-class"><input type="text" id="input-id" /></label>
Run the following code and look at the attrs
forwarding example in Vue 2:
Note: The code below may take a while to run. When the server starts, go to the app URL to see the output.
import "./examples/attrs-forwarding/attrs-forwarding";
-
A new Vue instance is created in the
attrs-forwarding.js
file. This Vue instance renders theAttrsForwardingExample
component. -
The
AttrsForwardingExample
component renders theAppInput
component with thelabel
,class
andinput-class
props and thev-model
directive. -
The
AppInput
component hasinheritAttrs
set tofalse
. This means that all values passed to the component that weren’t declared as props aren’t automatically forwarded to the root element. Instead, the ...