Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue.js Components

Vue.js Components

The component is one of the most powerful features of Vue.js.

Components can extend HTML elements and encapsulate reusable code.

The component system lets us build large applications from independent, reusable small components; almost any type of application interface can be abstracted into a component tree:

The syntax format for registering a global component is as follows:



Vue.component(tagName, options)

tagName is the component name and options are the configuration options. After registration, we can call the component as follows:





Global Components

All instances can use global components.

Global Component Example

Register a simple global component runoob and use it:

<

div

id

=

"

app

"

<

runoob

runoob

div

<

script

// register Vue.component('runoob', { template: '

<

h1

Custom component!

h1

' }) // create root instance new Vue({ el: '#app' })

script

Try it »

Local Components

We can also register local components in the instance options, so the component can only be used within this instance:

Local Component Example

Register a simple local component runoob and use it:

<

div

id

=

"

app

"

<

runoob

runoob

div

<

script

var Child = { template: '

<

h1

Custom component!

h1

' } // create root instance new Vue({ el: '#app', components: { //

<

runoob

will only be available in the parent template 'runoob': Child } })

script

Try it »


Prop

A prop is a custom attribute used by child components to receive data passed from the parent component.

The parent component passes data to the child component through props, and the child component must explicitly declare the "prop" using the props option:

Prop Example

<

div

id

=

"

app

"

<

child

message

=

"

hello!

"

child

div

<

script

// register Vue.component('child', { // declare props props: ['message'], // can also be used in the vm instance like "this.message" template: '

<

span

{{ message }}

span

' }) // create root instance new Vue({ el: '#app' })

script

Try it »

Dynamic Props

Similar to binding an HTML attribute to an expression with v-bind, you can also dynamically bind a prop's value to the parent component's data with v-bind. Whenever the parent's data changes, the change is propagated to the child component:

Prop Example

<

div

id

=

"

app

"

<

div

<

input

v-model

=

"

parentMsg

"

<

br

<

child

v-bind:message

=

"

parentMsg

"

child

div

div

<

script

// register Vue.component('child', { // declare props props: ['message'], // can also be used in the vm instance like "this.message" template: '

<

span

{{ message }}

span

' }) // create root instance new Vue({ el: '#app', data: { parentMsg: 'Parent component content' } })

script

Try it »

In the following example, the v-bind directive is used to pass to-do to each repeated component:

Prop Example

<

div

id

=

"

app

"

<

ol

<

todo-item

v-for

=

"

item in sites

"

v-bind:todo

=

"

item

"

todo-item

ol

div

<

script

Vue.component('todo-item', { props: ['todo'], template: '

<

li

{{ todo.text }}

li

' }) new Vue({ el: '#app', data: { sites: [ { text: 'Runoob' }, { text: 'Google' }, { text: 'Taobao' } ] } })

script

Try it »

Note: prop is one-way bound: when the parent component's property changes, it will be passed down to the child component, but not the other way around.

Prop Validation

A component can specify validation requirements for props.

To customize the prop validation, you can provide an object with validation requirements for the value in props, instead of a string array. For example:



Vue.component('my-component', {

  props: {

    // Basic type checking (`null` and `undefined` pass any type validation)

    propA: Number,

    // multiple possible types

    propB: [String, Number],

    // Required string

    propC: {

      type: String,

      required: true

    },

    // number with default value

    propD: {

      type: Number,

      default: 100

    },

    // object with default values

    propE: {

      type: Object,

      // Object or array defaults must be obtained from a factory function

      default: function () {

        return { message: 'hello' }

      }

    },

    // Custom validation function

    propF: {

      validator: function (value) {

        // This value must match one of the following strings

        return ['success', 'warning', 'danger'].indexOf(value) !== -1

      }

    }

  }

})

When prop validation fails, Vue (in the development build) will produce a console warning.

type can be the following native constructors:

type can also be a custom constructor, detected with instanceof.