Custom Components
Child Components
In simple applications, the window consists of just one component. However, when the window becomes complex, it may be a good idea to split it into a few separate child components. For example:
This creates a window component which contains two child components, one under another.
The child components must be imported and declared using the components
option of the parent component. Custom components declared like this can be used just like built-in components.
The root element of a child component can be a container, or even a single widget. For example:
Component Registration
Some components are used in many different places in the application, for example in different windows. Instead of importing them into every parent component that needs to use them, you can declare a global component.
This can be done by calling Vue.component()
in the main script of the application:
Now you can place the custom ButtonGroup component inside any other component.
See also Component Registration in the Vue.js documentation for more information.
Properties
You can use properties to pass data from the parent component to the child component. For example, let's define a custom component like this:
The props
section specifies that this component has two properties, okText
and cancelText
, and their values must be strings. You can use other JavaScript types, like Boolean
, Numeric
, Array
or Object
to validate the property.
Now you can use the ButtonGroup component like this:
Note that the kebab-cased names of attributes are mapped to the corresponding camelCased names of properties.
A property can have a default value:
It also can be marked as required:
You can pass a dynamic value to a property using v-bind
:
When using a boolean or numeric property, you must use the v-bind
directive even if you pass a constant value. Otherwise the value would be interpreted as a string. For example:
You can also omit the value to pass true
to a boolean property:
See also Props in the Vue.js documentation for more information.
Custom Events
Child components can use custom events to notify their parent:
The parent component can listen to these events using the v-on
directive:
You can also use events to pass data to the parent component:
The parent component can use the following code to update the value of its data property:
Note that the child component cannot simply modify the value
property, because that will not automatically update the data in the parent component.
The v-model
directive can also be used with custom components. By default, it binds the value
property and the input
event to the specified property of the parent component, so the above code is equivalent to:
When your window contains a complex hierarchy of custom components, passing data around using properties and events may become difficult to manage. In such case consider using a state management library, such as Vuex.
See also Custom Events in the Vue.js documentation for more information.
Slots
You can use slots to pass contents from the parent component to a child component. For example, let's assume that we have this simple child component:
We can use it like this:
The "About" text is inserted in place of the <slot>
element. The slot contents can include not just text, but also other elements, including built-in components and custom components.
See also Slots in the Vue.js documentation for more information.
Dynamic Components
The special <component>
element can be used to dynamically switch between components. For example, you can replace the entire contents of a window without having to close it and create a new window:
The currentComponent
property can contain either the name of a registered component, or the options object which defines a component.
See also Dynamic Components in the Vue.js documentation for more information.
Last updated