Component Options
Data
data
- Type:
Object | Function
- Restricton: Only accepts
Function
when used inVue.extend()
.
The data object for the Vue instance. It can be accessed as vm.$data
:
1 | var data = { a: 1 } |
The Vue instance will proxy access to all its properties, therefore you can manipulate the properties on the Vue instance and the changes get synced back to the actual data object:
1 | vm.a // -> 1 |
The object must be JSON-compliant (no circular references). You can use it just like an ordinary object, and it will look exactly the same when serialized with JSON.stringify
. You can also share it between multiple Vue instances.
A special case here is when using the data
option in Vue.extend()
. Since we don’t want nested objects to be shared by all instances created from that extended constructor, we must provide a function that returns a fresh copy of the default data:
1 | var MyComponent = Vue.extend({ |
Under the hood, Vue.js attaches a hidden property __ob__
and recursively converts the object’s enumerable properties into getters and setters to enable dependency collection. Properties with keys that starts with $
or _
are skipped.
methods
- Type:
Object
Methods to be mixed into the Vue instance. You can access these methods directly on the VM instance, or use them in directive expressions. All methods will have their this
context automatically bound to the Vue instance.
Example:
1 | var vm = new Vue({ |
computed
- Type:
Object
Computed properties to be mixed into the Vue instance. All getters and setters have their this
context automatically bound to the Vue instance.
Example:
1 | var vm = new Vue({ |
paramAttributes
- Type:
Array
An array of attribute names to be set on the Vue instance as initial data. Useful when passing data to a component.
Example:
1 | Vue.component('param-demo', { |
1 | <param-demo size="100" message="hello!"></param-demo> |
Param attributes can also contain interpolation tags. The interpolation will be evaluated against the parent, and under the hood they will be compiled as v-with
, which means when the value of the interpolated expression changes, the component’s corresponding property will also be updated:
1 | <param-demo message="{{parentMessage}}"></param-demo> |
Notes on hyphened attributes
HTML attribute names ignore upper and lower case differences, so we usually use hyphened attributes instead of camel case. There are some special cases when using paramAttributes
with attributes that contains hyphens:
If the attribute is a data attribute, the
data-
prefix will be auto stripped;If the attribute still contains dashes, it will be camelized. This is because it’s inconvenient to access top level properties containing dashes in templates: the expression
my-param
will be parsed as a minus expression unless you use the awkwardthis['my-param']
syntax.
This means a param attribute data-hello
will be set on the vm as vm.hello
; And my-param
will be set as vm.myParam
.
DOM
el
- Type:
String | HTMLElement | Function
- Restriction: only accepts type
Function
when used inVue.extend()
.
Provide the Vue instance with an existing DOM element. It can be a CSS selector string, an actual HTMLElement, or a function that returns an HTMLElement. The resolved element will be accessible as vm.$el
.
When used in Vue.extend
, a function must be provided so each instance gets a separately created element.
If the option is available at instantiation, the instance will immediately enter compilation; otherwise, the user will have to explicitly call vm.$mount()
to manually start the compilation.
template
- Type:
String
A string template to be inserted into vm.$el
. Any existing markup inside vm.$el
will be overwritten, unless content insertion points are present in the template. If the replace option is true
, the template will replace vm.$el
entirely.
If it starts with #
it will be used as a querySelector and use the selected element’s innerHTML and the template string. This allows the use of the common <script type="x-template">
trick to include templates.
Vue.js uses DOM-based templating. The compiler walks through DOM elements and looks for directives and creates data bindings. This means all Vue.js templates are parsable HTML that can be converted into actual DOM elements by the browser. Vue.js converts string templates into DOM fragments so they can be cloned when creating more Vue instances. If you want your templates to be valid HTML, you can configure the directive prefix to start with data-
.
replace
- Type:
Boolean
- Default:
false
- Restriction: only respected if the template option is also present.
Whether to replace the original vm.$el
with the template’s content instead of appending to it.
Lifecycle
All lifecycle hooks have their this
context bound to the Vue instance they belong to. The Vue instance will also fire corresponding events for each hook in the form of "hook:<hookName>"
. e.g. for created
, a "hook:created"
event will be fired.
created
- Type:
Function
Called synchronously after the instance is created. At this stage, the instance has finished processing the options which means the following have been set up: data observation, computed properties, methods, watch/event callbacks. However, DOM compilation has not been started, and the $el
property will not be available yet.
beforeCompile
- Type:
Function
Called right before the compilation starts.
compiled
- Type:
Function
Called after the compilation is finished. At this stage all directives have been linked so data changes will trigger DOM updates. However, $el
is not guaranteed to have been inserted into the document yet.
ready
- Type:
Function
Called after compilation and the $el
is inserted into the document for the first time. Note this insertion must be executed via Vue (with methods like vm.$appendTo()
or as a result of a directive update) to trigger the ready
hook.
attached
- Type:
Function
Called when vm.$el
is attached to DOM by a directive or a VM instance method such as $appendTo()
. Direct manipulation of vm.$el
will not trigger this hook.
detached
- Type:
Function
Called when vm.$el
is removed from the DOM by a directive or a VM instance method. Direct manipulation of vm.$el
will not trigger this hook.
beforeDestroy
- Type:
Function
Called right before a Vue instance is destroyed. At this stage the instance is still fully functional.
destroyed
- Type:
Function
Called after a Vue instance has been destroyed. When this hook is called, all bindings and directives of the Vue instance have been unbound and all child Vue instances have also been destroyed.
Note if there is a leaving transition, the destroyed
hook is called after the transition has finished.
Assets
These are private assets that will be available only to this Vue instance and its children during compilation.
directives
- Type:
Object
A hash of directives to be made available to the Vue instance. For details on how to write a custom directive, see Writing Custom Directives.
filters
- Type:
Object
A hash of filters to be made available to the Vue instance. For details on how to write a custom filter, see Writing Custom Filters.
components
- Type:
Object
A hash of components to be made available to the Vue instance. For details on how to extend and compose Vue instances, see Component System.
partials
- Type:
Object
A hash of partials to be made available to the Vue instance. Also see v-partial.
transitions
- Type:
Object
A hash of transitions to be made available to the Vue instance. For details see the guide on Transitions.
Others
inherit
- Type:
Boolean
- Default:
false
Whether to inherit parent scope data. Set it to true
if you want to create a component that inherits parent scope. When inherit
is set to true
, you can:
- Bind to parent scope properties in the component template;
- Directly access parent properties on the component instance itself, via prototypal inheritance.
One important thing to know when using inherit: true
is that the child can also set parent properties, because all Vue instance data properties are getter/setters.
Example:
1 | var parent = new Vue({ |
events
An object where keys are events to listen for and values are the corresponding callbacks. Note these are Vue events rather than DOM events. The value can also be a string of a method name. The Vue instance will call $on()
for each entry in the object at instantiation.
Example:
1 | var vm = new Vue({ |
watch
- Type:
Object
An object where keys are expressions to watch and values are the corresponding callbacks. The value can also be a string of a method name. The Vue instance will call $watch()
for each entry in the object at instantiation.
Example:
1 | var vm = new Vue({ |
mixins
- Type:
Array
The mixins
option accepts an array of mixin objects. These mixin objects can contain instance options just like normal instance objects, and they will be merged against the eventual options using the same option merging logic in Vue.extend()
. e.g. If your mixin contains a created hook and the component itself also has one, both functions will be called.
Example:
1 | var mixin = { |
name
- Type:
String
- Restrctions: only respected when used in
Vue.extend()
.
When inspecting an extended Vue component in the console, the default constructor name is VueComponent
, which isn’t very informative. By passing in an optional name
option to Vue.extend()
, you will get a better inspection output so that you know which component you are looking at. The string will be camelized and used as the component’s constructor name.
Example:
1 | var Ctor = Vue.extend({ |