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

Vue Instance Options

Vue Instance Options

Options in Vue are the different options available on a Vue instance when using the Options API.

Some common options for a Vue instance:

el

Mount point, used to specify the DOM element the Vue instance mounts to:

Example

const

app

=

Vue.

createApp

(

{

 

// options

}

)

;

app.

mount

(

'#app'

)

;

data

Example

const

app

=

Vue.

createApp

(

{

  data

(

)

{

   

return

{

      message

:

'Hello Vue!'

   

}

;

 

}

}

)

;

computed

Example

const

app

=

Vue.

createApp

(

{

  data

(

)

{

   

return

{

      firstName

:

'John'

,

      lastName

:

'Doe'

   

}

;

 

}

,

  computed

:

{

    fullName

(

)

{

     

return

`$

{

this

.

firstName

}

$

{

this

.

lastName

}

`

;

   

}

 

}

}

)

;

methods

Example

const

app

=

Vue.

createApp

(

{

  data

(

)

{

   

return

{

      count

:

0

   

}

;

 

}

,

  methods

:

{

    increment

(

)

{

     

this

.

count

++;

   

}

 

}

}

)

;

watch

Example

const

app

=

Vue.

createApp

(

{

  data

(

)

{

   

return

{

      message

:

'Hello Vue!'

   

}

;

 

}

,

  watch

:

{

    message

(

newValue

,

oldValue

)

{

      console.

log

(

`Message changed from $

{

oldValue

}

to $

{

newValue

}

`

)

;

   

}

 

}

}

)

;

props

In the following example, MyComponent receives a title prop and uses it in the template:

Example

const

MyComponent

=

{

  props

:

[

'title'

]

,

  template

:

'

{{ title }}

'

}

;

const

app

=

Vue.

createApp

(

{

  components

:

{

   

'my-component'

:

MyComponent

 

}

,

  template

:

''

}

)

;

app.

mount

(

'#app'

)

;

template

Example

const

app

=

Vue.

createApp

(

{

  data

(

)

{

   

return

{

      message

:

'Hello Vue!'

   

}

;

 

}

,

  template

:

'

{{ message }}
'

}

)

;

components

components is used to register local components.

Example

const

ChildComponent

=

{

  template

:

'

A child component
'

}

;

const

app

=

Vue.

createApp

(

{

  components

:

{

   

'child-component'

:

ChildComponent

 

}

,

  template

:

''

}

)

.

mount

(

'#app'

)

;

provide / inject

The parent component provides data, and the child component injects and uses it.

Example

const

ChildComponent

=

{

  inject

:

[

'message'

]

,

  template

:

'

{{ message }}
'

}

;

const

app

=

Vue.

createApp

(

{

  provide

(

)

{

   

return

{

      message

:

'Provided Message'

   

}

;

 

}

,

  components

:

{

   

'child-component'

:

ChildComponent

 

}

,

  template

:

''

}

)

.

mount

(

'#app'

)

;


Lifecycle Hooks

beforeCreate

Called before the instance is initialized.

Example

const

app

=

Vue.

createApp

(

{

  beforeCreate

(

)

{

    console.

log

(

'beforeCreate'

)

;

 

}

}

)

.

mount

(

'#app'

)

;

created

Called after the instance is created.

Example

const

app

=

Vue.

createApp

(

{

  created

(

)

{

    console.

log

(

'created'

)

;

 

}

}

)

.

mount

(

'#app'

)

;

beforeMount

Called before mounting.

Example

const

app

=

Vue.

createApp

(

{

  beforeMount

(

)

{

    console.

log

(

'beforeMount'

)

;

 

}

}

)

.

mount

(

'#app'

)

;

mounted

Called after the instance is mounted.

Example

const

app

=

Vue.

createApp

(

{

  mounted

(

)

{

    console.

log

(

'mounted'

)

;

 

}

}

)

.

mount

(

'#app'

)

;

beforeUpdate

Called before data updates.

Example

const

app

=

Vue.

createApp

(

{

  data

(

)

{

   

return

{

      message

:

'Hello'

   

}

;

 

}

,

  beforeUpdate

(

)

{

    console.

log

(

'beforeUpdate'

)

;

 

}

}

)

.

mount

(

'#app'

)

;

updated

Called after data updates.

Example

const

app

=

Vue.

createApp

(

{

  data

(

)

{

   

return

{

      message

:

'Hello'

   

}

;

 

}

,

  updated

(

)

{

    console.

log

(

'updated'

)

;

 

}

}

)

.

mount

(

'#app'

)

;

beforeUnmount

Called before the instance is unmounted.

Example

const

app

=

Vue.

createApp

(

{

  beforeUnmount

(

)

{

    console.

log

(

'beforeUnmount'

)

;

 

}

}

)

.

mount

(

'#app'

)

;

unmounted

Called after the instance is unmounted.

Example

const

app

=

Vue.

createApp

(

{

  unmounted

(

)

{

    console.

log

(

'unmounted'

)

;

 

}

}

)

.

mount

(

'#app'

)

;


Other Options

directives

Registers local directives.

Example

const

app

=

Vue.

createApp

(

{

  directives

:

{

    focus

:

{

      mounted

(

el

)

{

        el.

focus

(

)

;

     

}

   

}

 

}

,

  template

:

''

}

)

.

mount

(

'#app'

)

;

mixins

Mixin object.

Example

const

myMixin

=

{

  created

(

)

{

    console.

log

(

'Mixin created'

)

;

 

}

}

;

const

app

=

Vue.

createApp

(

{

  mixins

:

[

myMixin

]

,

  created

(

)

{

    console.

log

(

'Component created'

)

;

 

}

}

)

.

mount

(

'#app'

)

;

extends

Extends another component.

Example

const

BaseComponent

=

{

  created

(

)

{

    console.

log

(

'BaseComponent created'

)

;

 

}

}

;

const

app

=

Vue.

createApp

(

{

 

extends

:

BaseComponent

,

  created

(

)

{

    console.

log

(

'Extended component created'

)

;

 

}

}

)

.

mount

(

'#app'

)

;

inheritAttrs

Whether to inherit the parent component's attributes.

Example

const

MyComponent

=

{

  inheritAttrs

:

false

,

  props

:

[

'title'

]

,

  template

:

'

{{ title }}

'

}

;

const

app

=

Vue.

createApp

(

{

  components

:

{

   

'my-component'

:

MyComponent

 

}

,

  template

:

''

}

)

.

mount

(

'#app'

)

;

setup

Used for the Composition API.

Example

const

app

=

Vue.

createApp

(

{

  setup

(

)

{

   

const

message

=

Vue.

ref

(

'Hello Vue 3!'

)

;

   

return

{

message

}

;

 

}

,

  template

:

'

{{ message }}

'

}

)

.

mount

(

'#app'

)

;

expose

In Vue 3's Composition API, when using the setup function to define component logic, you can use the expose method to explicitly specify the properties and methods to expose externally.

Here is a simple example showing how to use expose to expose properties and methods in a Vue 3 component:

Parent component (ParentComponent.vue)

Child component (ChildComponent.vue)

Explanation:

  1. Parent component:

  2. In the parent component, using ref creates a reference to the child component child.
  3. in callChildMethod ; in the method, through child.value access the child component instance and call the method exposed by the child component exposedMethod.
  4. - In the parent component, `ref` creates a reference `child` to the child component. - In the `callChildMethod` method, `child.value` accesses the child component instance and calls the exposed method `exposedMethod`.
    1. Child component:

    2. In the child component's setup function, through ref creates a reactive data exposedData.
    3. defines a method exposedMethod, used to demonstrate how to expose a method.
    4. Use expose The method explicitly exposes exposedData and exposedMethod to the parent component.
    5. Return exposedData, so it can be used in the template.
    6. - In the child component's `setup` function, `ref` creates a reactive data `exposedData`. - A method `exposedMethod` is defined to demonstrate how to expose a method. - The `expose` method explicitly exposes `exposedData` and `exposedMethod` to the parent component. - Returns `exposedData` so it can be used in the template.