Vue.js (original) (raw)

Component Instance

INFO

This page documents the built-in properties and methods exposed on the component public instance, i.e. this.

All properties listed on this page are readonly (except nested properties in $data).

$data

The object returned from the data option, made reactive by the component. The component instance proxies access to the properties on its data object.

interface ComponentPublicInstance {  
  $data: object  
}  

$props

An object representing the component's current, resolved props.

interface ComponentPublicInstance {  
  $props: object  
}  

$el

The root DOM node that the component instance is managing.

interface ComponentPublicInstance {  
  $el: any  
}  

$options

The resolved component options used for instantiating the current component instance.

interface ComponentPublicInstance {  
  $options: ComponentOptions  
}  
const app = createApp({  
  customOption: 'foo',  
  created() {  
    console.log(this.$options.customOption) // => 'foo'  
  }  
})  

$parent

The parent instance, if the current instance has one. It will be null for the root instance itself.

interface ComponentPublicInstance {  
  $parent: ComponentPublicInstance | null  
}  

$root

The root component instance of the current component tree. If the current instance has no parents this value will be itself.

interface ComponentPublicInstance {  
  $root: ComponentPublicInstance  
}  

$slots

An object representing the slots passed by the parent component.

interface ComponentPublicInstance {  
  $slots: { [name: string]: Slot }  
}  
type Slot = (...args: any[]) => VNode[]  

$refs

An object of DOM elements and component instances, registered via template refs.

interface ComponentPublicInstance {  
  $refs: { [name: string]: Element | ComponentPublicInstance | null }  
}  

$attrs

An object that contains the component's fallthrough attributes.

interface ComponentPublicInstance {  
  $attrs: object  
}  

$watch()

Imperative API for creating watchers.

interface ComponentPublicInstance {  
  $watch(  
    source: string | (() => any),  
    callback: WatchCallback,  
    options?: WatchOptions  
  ): StopHandle  
}  
type WatchCallback<T> = (  
  value: T,  
  oldValue: T,  
  onCleanup: (cleanupFn: () => void) => void  
) => void  
interface WatchOptions {  
  immediate?: boolean // default: false  
  deep?: boolean // default: false  
  flush?: 'pre' | 'post' | 'sync' // default: 'pre'  
  onTrack?: (event: DebuggerEvent) => void  
  onTrigger?: (event: DebuggerEvent) => void  
}  
type StopHandle = () => void  
this.$watch('a', (newVal, oldVal) => {})  

Watch a dot-delimited path:
js

this.$watch('a.b', (newVal, oldVal) => {})  

Using getter for more complex expressions:
js

this.$watch(  
  // every time the expression `this.a + this.b` yields  
  // a different result, the handler will be called.  
  // It's as if we were watching a computed property  
  // without defining the computed property itself.  
  () => this.a + this.b,  
  (newVal, oldVal) => {}  
)  

Stopping the watcher:
js

const unwatch = this.$watch('a', cb)  
// later...  
unwatch()  

$emit()

Trigger a custom event on the current instance. Any additional arguments will be passed into the listener's callback function.

interface ComponentPublicInstance {  
  $emit(event: string, ...args: any[]): void  
}  
export default {  
  created() {  
    // only event  
    this.$emit('foo')  
    // with additional arguments  
    this.$emit('bar', 1, 2, 3)  
  }  
}  

$forceUpdate()

Force the component instance to re-render.

interface ComponentPublicInstance {  
  $forceUpdate(): void  
}  

$nextTick()

Instance-bound version of the global nextTick().

interface ComponentPublicInstance {  
  $nextTick(callback?: (this: ComponentPublicInstance) => void): Promise<void>  
}