Component System
Using Components
Vue.js allows you to treat extended Vue subclasses as reusable components that are conceptually similar to Web Components, without requiring any polyfills. To create a component, just create a subclass constructor of Vue using Vue.extend()
:
1 | // Extend Vue to get a reusable constructor |
Most of the options that can be passed into the Vue constructor can be used in Vue.extend()
, however, there are two special cases, data
and el
. Since each Vue instance should have its own $data
and $el
, we obviously don’t want the value we passed into Vue.extend()
to be shared across all instances created from that constructor. So when you want to define how a component should initalize its default data or element, you should pass in a function instead:
1 | var ComponentWithDefaultData = Vue.extend({ |
Then, you can register that constructor with Vue.component()
:
1 | // Register the constructor with id: my-component |
To make things easier, you can also directly pass in the option object instead of an actual constructor. Vue.component()
will implicitly call Vue.extend()
for you if it receives an object:
1 | // Note: this method returns the global Vue, |
Then you can use the registered component in a parent instance’s template (make sure the component is registered before you instantiate your root Vue instance):
1 | <!-- inside parent template --> |
Which will render:
1 | <p>A custom component!</p> |
You don’t have to register every component globally. You can limit a component’s availability to another component and its descendents by passing it in with the components
option (this encapsulation also applies to other assets such as directives and filters):
1 | var Parent = Vue.extend({ |
It is important to understand the difference between Vue.extend()
and Vue.component()
. Since Vue
itself is a constructor, Vue.extend()
is a class inheritance method. Its task is to create a sub-class of Vue
and return the constructor. Vue.component()
, on the other hand, is an asset registration method similar to Vue.directive()
and Vue.filter()
. Its task is to associate a given constructor with a string ID so Vue.js can pick it up in templates. When directly passing in options to Vue.component()
, it calls Vue.extend()
under the hood.
Vue.js supports two API styles for using components: the imperative, constructor-based API, and the declarative, template-based API. If you are confused, think about how you can create an image element with new Image()
, or with an <img>
tag. Each is useful in its own right and Vue.js provides both for maximum flexibility.
The table
element has restrictions on what elements can appear inside it, so custom elements will be hoisted out and not render properly. In those cases you can use the component directive syntax: <tr v-component="my-component"></tr>
.
Data Flow
Passing Data with Props
By default, components have isolated scope. This means you cannot reference parent data in a child component’s template. In order to pass data to child components with isolated scope, we need to use props
.
A “prop” is a field on a component’s data that is expected to be received from its parent component. A child component needs to explicitly declare the props it expects to receive using the props
option:
1 | Vue.component('child', { |
Then, we can pass data to it like so:
1 | <child msg="hello!"></child> |
Result:
camelCase vs. Hyphenated
HTML attributes are case-insensitive. When using camelCased prop names as attributes, you need to use their hyphenated equivalents:
1 | Vue.component('child', { |
1 | <!-- important: use hyphenated names! --> |
Dynamic Props
We can also pass down dynamic data from the parent. For example:
1 | <div> |
Result:
It is also possible to expose $data
as a prop. The passed in value must be an Object and will replace the component’s default $data
.
Passing Callbacks as Props
It is also possible to pass down a method or a statement as a callback to a child component. This enables declarative, decoupled parent-child communication:
1 | Vue.component('parent', { |
1 | <!-- in parent's template --> |
Prop Binding Types
By default, all props form a one-way-down binding between the child property and the parent one: when the parent property updates, it will be synced down to the child, but not the other way around. This default is meant to prevent child components from accidentally mutating the parent’s state, which can make your app’s data flow harder to reason about. However, it is also possible to explicitly enforce a two-way or a one-time binding:
Compare the syntax:
1 | <!-- default, one-way-down binding --> |
The two-way binding will sync the change of child’s msg
property back to the parent’s parentMsg
property. The one-time binding, once set up, will not sync future changes between the the parent and the child.
Note that if the prop being passed down is an Object or an Array, it is passed by reference. Mutating the Object or Array itself inside the child will affect parent state, regardless of the binding type you are using.
Prop Specification
It is possible for a component to specify the requirements for the props it is receiving. This is useful when you are authoring a component that is intended to be used by others, as these prop validation requirements essentially constitute your component’s API, and ensure your users are using your component correctly. Instead of defining the props as strings, you can use Objects that contain validation requirements:
1 | Vue.component('example', { |
The type
can be one of the following native constructors:
- String
- Number
- Boolean
- Function
- Object
- Array
In addition, type
can also be a custom constructor function and the assertion will be made with an instanceof
check.
When a prop validation fails, Vue will refuse to set the value on the child component, and throw a warning if using the development build.
Inheriting Parent Scope
If you want, you can also use the inherit: true
option for your child component to make it prototypally inherit all parent properties:
1 | var parent = new Vue({ |
Note this comes with a caveat: because data properties on Vue instances are getter/setters, setting child.a = 2
will change parent.a
instead of creating a new property on the child shadowing the parent one:
1 | child.a = 4 |
A Note on Scope
When a component is used in a parent template, e.g.:
1 | <!-- parent template --> |
The directives here (v-show
and v-on
) will be compiled in the parent’s scope, so the value of active
and onClick
will be resolved against the parent. Any directives/interpolations inside the child’s template will be compiled in the child’s scope. This ensures a cleaner separation between parent and child components.
Read more details on Component Scope.
Component Lifecycle
Every component, or Vue instance, has its own lifecycle: it will be created, compiled, attached or detached, and finally destroyed. At each of these key moments the instance will emit corresponding events, and when creating an instance or defining a component, we can pass in lifecycle hook functions to react to these events. For example:
1 | var MyComponent = Vue.extend({ |
Check out the API reference for a full list of lifecycle hooks that are available.
Dynamic Components
You can dynamically switch between components to achieve “page swapping” by using the reserved <component>
element:
1 | new Vue({ |
1 | <component is="{{currentView}}"> |
If you want to keep the switched-out components alive so that you can preserve its state or avoid re-rendering, you can add a keep-alive
directive param:
1 | <component is="{{currentView}}" keep-alive> |
Transition Control
There are two additional param attributes that allows advanced control of how components should be rendered / transitioned.
wait-for
An event name to wait for on the incoming child component before inserting it into the DOM. This allows you to wait for asynchronous data to be loaded before triggering the transition and avoid displaying empty content.
This attribute can be used both on static and dynamic components. Note: for dynamic components, all components that will potentially get rendered must $emit
the awaited event, otherwise they will never get inserted.
Example:
1 | <!-- static --> |
1 | // component definition |
transition-mode
The transition-mode
param attribute allows you to specify how the transition between two dynamic components should be executed.
By default, the transitions for incoming and outgoing components happen simultaneously. This attribute allows you to configure two other modes:
in-out
: New component transitions in first, current component transitions out after incoming transition has finished.out-in
: Current component transitions out first, new componnent transitions in after outgoing transition has finished.
Example
1 | <!-- fade out first, then fade in --> |
List and Components
For an Array of Objects, you can combine a component with v-repeat
. In this case, for each Object in the Array, a child component will be created using that Object as its $data
, and the specified component as the constructor.
1 | <ul id="list-example"> |
1 | new Vue({ |
Result:
Repeat Component with alias
The alias syntax also works when using a component, and the repeated data will be set as a property on the component using the alias as the key:
1 | <ul id="list-example"> |
Note that once you use a component with v-repeat
, the same scoping rules apply to the other directives on the component container element. As a result, you won’t be able to access $index
in the parent template; it will become only available inside the component’s own template.
Alternatively, you can use a <template>
block repeat to create an intermediate scope, but in most cases it’s better to use $index
inside the component.
Child Reference
Sometimes you might need to access nested child components in JavaScript. To enable that you have to assign a reference ID to the child component using v-ref
. For example:
1 | <div id="parent"> |
1 | var parent = new Vue({ el: '#parent' }) |
When v-ref
is used together with v-repeat
, the value you get will be an Array containing the child components mirroring the data Array.
Event System
Although you can directly access a Vue instance’s children and parent, it is more convenient to use the built-in event system for cross-component communication. It also makes your code less coupled and easier to maintain. Once a parent-child relationship is established, you can dispatch and trigger events using each component’s event instance methods.
1 | var parent = new Vue({ |
Private Assets
Sometimes a component needs to use assets such as directives, filters and its own child components, but might want to keep these assets encapsulated so the component itself can be reused elsewhere. You can do that using the private assets instantiation options. Private assets will only be accessible by the instances of the owner component, components that inherit from it, and its child components in the view hierarchy.
1 | // All 5 types of assets |
You can prohibit child components from accessing a parent component’s private assets by setting Vue.config.strict = true
.
Alternatively, you can add private assets to an existing Component constructor using a chaining API similar to the global asset registration methods:
1 | MyComponent |
Asset Naming Convention
Some assets, such as components and directives, appear in templates in the form of HTML attributes or HTML custom tags. Since HTML attribute names and tag names are case-insensitive, we often need to name our assets using dash-case instead of camelCase. Starting in 0.12.11, it is now supported to name your assets using camelCase or PascalCase, and use them in templates with dash-case.
Example
1 | // in a component definition |
1 | <!-- use dash case in templates --> |
This works nicely with ES6 object literal shorthand:
1 | // PascalCase |
Content Insertion
When creating reusable components, we often need to access and reuse the original content in the hosting element, which are not part of the component (similar to the Angular concept of “transclusion”.) Vue.js implements a content insertion mechanism that is compatible with the current Web Components spec draft, using the special <content>
element to serve as insertion points for the original content.
Important: transcluded contents are compiled in the parent component’s scope, not in the child’s scope.
Single Insertion Point
When there is only one <content>
tag with no attributes, the entire original content will be inserted at its position in the DOM and replaces it. Anything originally inside the <content>
tags is considered fallback content. Fallback content will only be displayed if the hosting element is empty and has no content to be inserted. For example:
Template for my-component
:
1 | <div> |
Parent markup that uses the component:
1 | <my-component> |
The rendered result will be:
1 | <div> |
Multiple Insertion Points
<content>
elements have a special attribute, select
, which expects a CSS selector. You can have multiple <content>
insertion points with different select
attributes, and each of them will be replaced by the elements matching that selector from the original content.
Starting in 0.11.6, <content>
selectors can only match top-level children of the host node. This keeps the behavior consistent with the Shadow DOM spec and avoids accidentally selecting unwanted nodes in nested transclusions.
For example, suppose we have a multi-insertion
component with the following template:
1 | <div> |
Parent markup:
1 | <multi-insertion> |
The rendered result will be:
1 | <div> |
The content insertion mechanism provides fine control over how original content should be manipulated or displayed, making components extremely flexible and composable.
Inline Template
In 0.11.6, a special param attribute for components is introduced: inline-template
. When this param is present, the component will use its inner content as its template rather than transclusion content. This allows more flexible template-authoring.
1 | <my-component inline-template> |
Async Components
Async Components are only supported in Vue ^0.12.0.
In large applications, we may need to divide the app into smaller chunks, and only load a component from the server when it is actually needed. To make that easier, Vue.js allows you to define your component as a factory function that asynchronously resolves your component definition. Vue.js will only trigger the factory function when the component actually needs to be rendered, and will cache the result for future re-renders. For example:
1 | Vue.component('async-example', function (resolve, reject) { |
The factory function receives a resolve
callback, which should be called when you have retrived your component definition from the server. You can also call reject(reason)
to indicate the load has failed. The setTimeout
here is simply for demonstration; How to retrieve the component is entirely up to you. One recommended approach is to use async components together with Webpack’s code-splitting feature:
1 | Vue.component('async-webpack-example', function (resolve) { |
Next: Applying Transition Effects.