# Application Config

Every Vue application exposes a config object that contains the configuration settings for that application:

const app = Vue.createApp({})

app.config = {...}
Application Config description H5 App Description
errorHandler Assign a handler for uncaught errors during component render function and watchers. details (opens new window)
warnHandler Assign a custom handler for runtime Vue warnings. details (opens new window)
globalProperties Adds a global property that can be accessed in any component instance inside the application.details (opens new window)
isCustomElement Specifies a method to recognize custom elements defined outside of Vue.details (opens new window)
optionMergeStrategies Define merging strategies for custom options.details (opens new window)
performance Set this to true to enable component init, compile, render and patch performance tracing in the browser devtool performance/timeline panel. details (opens new window) x Only supported in the web environment.

# Application API

In Vue 3, APIs that globally mutate Vue's behavior are now moved to application instances created by the new createApp method. In addition, their effects are now scoped to that specific application's instance:

import { createApp } from 'vue'

const app = createApp({})

Calling createApp returns an application instance. This instance provides an application context. The entire component tree mounted by the application instance share the same context, which provides the configurations that were previously "global" in Vue 2.x.

In addition, since the createApp method returns the application instance itself, you can chain other methods after it which can be found in the following sections.

Application API description H5 App
component Register or retrieve a global component. details (opens new window)
config An object containing application configurations.details (opens new window)
directive Register or retrieve a global directive.details (opens new window)
mixin Apply a mixin in the whole application scope. details (opens new window)
provide Sets a value that can be injected into all components within the application. details (opens new window)
use Install a Vue.js plugin.details (opens new window)

# Global API

Global API description H5 App
createApp Returns an application instance which provides an application context. The entire component tree mounted by the application instance share the same context.details (opens new window)
h eturns a "virtual node", usually abbreviated to VNode: a plain object which contains information describing to Vue what kind of node it should render on the page, including descriptions of any child nodes. details (opens new window) x
defineComponent Implementation-wise defineComponent does nothing but return the object passed to it. However, in terms of typing, the returned value has a synthetic type of a constructor for manual render function, TSX and IDE tooling support.details (opens new window) x
defineAsyncComponent Creates an async component that will be loaded only when it's necessary.details (opens new window) x
resolveComponent Allows resolving a component by its name, if it is available in the current application instance.Returns a Component or the argument name when not found.details (opens new window) x
resolveDynamicComponent Allows resolving a component by the same mechanism that <component :is=""> employs.details (opens new window) x
resolveDirective Allows resolving a directive by its name, if it is available in the current application instance.Returns a Directive or undefined when not found.details (opens new window) x
withDirectives Allows applying directives to a VNode. Returns a VNode with the applied directives.details (opens new window) x
createRenderer The createRenderer function accepts two generic arguments: HostNode and HostElement, corresponding to Node and Element types in the host environment.details (opens new window) x
nextTick Defer the callback to be executed after the next DOM update cycle. Use it immediately after you’ve changed some data to wait for the DOM update.details (opens new window) x

# Options/Data

Data description H5 App
data The function that returns a data object for the component instance. details (opens new window)
props A list/hash of attributes that are exposed to accept data from the parent component. It has an Array-based simple syntax and an alternative Object-based syntax that allows advanced configurations such as type checking, custom validation and default values.details (opens new window)
computed Computed properties to be mixed into the component instance. All getters and setters have their this context automatically bound to the component instance.details (opens new window)
methods Methods to be mixed into the component 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 component instance.details (opens new window)
watch An object where keys are reactive properties to watch — examples include data or computed properties — and values are the corresponding callbacks. The value can also be a string of a method name, or an Object that contains additional options. details (opens new window)
emits A list/hash of custom events that can be emitted from the component. It has an array-based simple syntax and an alternative Object-based syntax that allows to configure an event validation.details (opens new window)

# Options/DOM

DOM description H5 App Description
template A string template to be used as the markup for the component instance.details (opens new window) x The Vue used by the uni-app is a runtime only version
render An alternative to string templates allowing you to leverage the full programmatic power of JavaScript.details (opens new window) x -

# Options/Lifecycle hooks

Lifecycle hooks description H5 App
beforeCreate Called synchronously immediately after the instance has been initialized, before data observation and event/watcher setup.details (opens new window)
created Called synchronously after the instance is created.details (opens new window)
beforeMount Called right before the mounting begins: the render function is about to be called for the first time.details (opens new window)
mounted Called after the instance has been mounted, where element, passed to app.mount is replaced by the newly created vm.$el.details (opens new window)
beforeUpdate Called when data changes, before the DOM is patched. details (opens new window)
updated Called after a data change causes the virtual DOM to be re-rendered and patched.details (opens new window)
activated Called when a kept-alive component is activated.details (opens new window)
deactivated Called when a kept-alive component is deactivated.details (opens new window)
beforeUnmount Called right before a component instance is unmounted. At this stage the instance is still fully functional.details (opens new window)
unmounted Called after a component instance has been unmounted. When this hook is called, all directives of the component instance have been unbound, all event listeners have been removed, and all child component instance have also been unmounted.details (opens new window)
errorCaptured Called when an error from any descendent component is captured. The hook receives three arguments: the error, the component instance that triggered the error, and a string containing information on where the error was captured.details (opens new window)
renderTracked Called when virtual DOM re-render is tracked. The hook receives a debugger event as an argument. This event tells you what operation tracked the component and the target object and key of that operation.details (opens new window)
renderTriggered Called when virtual DOM re-render is triggered. Similarly to renderTracked, receives a debugger event as an argument. This event tells you what operation triggered the re-rendering and the target object and key of that operation.details (opens new window)

# Options/Assets

Assets description H5 App
directives A hash of directives to be made available to the component instance.details (opens new window)
components A hash of components to be made available to the component instance.details (opens new window)

# Options/Composition

Composition description H5 App
mixins The mixins option accepts an array of mixin objects. These mixin objects can contain instance options like normal instance objects, and they will be merged against the eventual options using the certain option merging logic. details (opens new window)
extends Allows one component to extend another, inheriting its component options.details (opens new window)
provide / inject This pair of options are used together to allow an ancestor component to serve as a dependency injector for all its descendants, regardless of how deep the component hierarchy is, as long as they are in the same parent chain. details (opens new window)
setup The setup function is a new component option. It serves as the entry point for using the Composition API inside components.details (opens new window)

# Options/Misc

Misc description H5 App
name Allow the component to recursively invoke itself in its template. Note that when a component is registered globally with app.component, the global ID is automatically set as its name.details (opens new window)
delimiters Sets the delimiters used for text interpolation within the template.details (opens new window) x
inheritAttrs By default, parent scope attribute bindings that are not recognized as props will "fallthrough". This means that when we have a single-root component, these bindings will be applied to the root element of the child component as normal HTML attributes. When authoring a component that wraps a target element or another component, this may not always be the desired behavior. By setting inheritAttrs to false, this default behavior can be disabled. details (opens new window)

# Instance Properties

Instance Properties description H5 App Description
$data The data object that the component instance is observing. The component instance proxies access to the properties on its data object.details (opens new window)
$props An object representing the current props a component has received. The component instance proxies access to the properties on its props object.details (opens new window)
$el The root DOM element that the component instance is managing.details (opens new window) x
$options The instantiation options used for the current component instance. This is useful when you want to include custom properties in the options.details (opens new window)
$parent The parent instance, if the current instance has one.details (opens new window) Built-in tags such as view and text are implemented as Vue components. $parent will get these to the built-in components, but it will cause this.$parent to be inconsistent with other platforms. The solution is to use this.$parent.$parent to get the built-in components or customize the root node of the component from view to div.
$root The root component instance of the current component tree. If the current instance has no parents this value will be itself.details (opens new window)
$slots Used to programmatically access content distributed by slots. Each named slot has its own corresponding property (e.g. the contents of v-slot:foo will be found at this.$slots.foo()).details (opens new window) x
$refs An object of DOM elements and component instances, registered with ref attributes.details (opens new window) Non-H5 sides can only be used to get custom components, and cannot be used to get built-in component instances (such as view, text)
$attrs Contains parent-scope attribute bindings and events that are not recognized (and extracted) as component props or custom events.details (opens new window) -

# Instance Methods

Instance Methods description H5 App
$watch Watch a reactive property or a computed function on the component instance for changes.details (opens new window)
$emit Trigger an event on the current instance. Any additional arguments will be passed into the listener's callback function.details (opens new window)
$forceUpdate Force the component instance to re-render. Note it does not affect all child components, only the instance itself and child components with inserted slot content.details (opens new window)
$nextTick Defer the callback to be executed after the next DOM update cycle. Use it immediately after you've changed some data to wait for the DOM update.details (opens new window)

# Directives

Directives description H5 App Description
v-text Updates the element's textContent (opens new window) (opens new window). details (opens new window)
v-html Updates the element's innerHTML (opens new window). details (opens new window) x
v-show Toggles the element's display CSS property based on the truthy-ness of the expression value. details (opens new window)
v-if Conditionally render the element based on the truthy-ness of the expression value. details (opens new window)
v-else Denote the "else block" for v-if or a v-if/v-else-if chain.details (opens new window)
v-else-if Denote the "else if block" for v-if. Can be chained. details (opens new window)
v-for Render the element or template block multiple times based on the source data. details (opens new window)
v-on Attaches an event listener to the element. details (opens new window)
v-bind Dynamically bind one or more attributes, or a component prop to an expression. details (opens new window)
v-model Create a two-way binding on a form input element or a component. details (opens new window)
v-slot Denote named slots or slots that expect to receive props. details (opens new window)
v-pre Skip compilation for this element and all its children. details (opens new window)
v-cloak This directive will remain on the element until the associated component instance finishes compilation. details (opens new window) x
v-once Render the element and component once only. details (opens new window)
v-is When used by the template in DOM, the template is subject to the native HTML parsing rules. Details (opens new window) x -

# Special Attributes

Special Attributes description H5 App Description
key The key special attribute is primarily used as a hint for Vue's virtual DOM algorithm to identify VNodes when diffing the new list of nodes against the old list. details (opens new window)
ref ref is used to register a reference to an element or a child component. details (opens new window) Non-H5 sides can only be used to get custom components, and cannot be used to get built-in component instances (such as view, text)
is Used fordynamic components (opens new window)details (opens new window) -

# Built-In Components

Built-In Components description H5 App
component A "meta component" for rendering dynamic components. The actual component to render is determined by the is prop. details (opens new window)
transition <transition> serve as transition effects for single element/component. details (opens new window) x
transition-group <transition-group> provides transition effects for multiple elements/components. details (opens new window) x
keep-alive When wrapped around a dynamic component, <keep-alive> caches the inactive component instances without destroying them. Primarily used to preserve component state or avoid re-rendering. details (opens new window) x
slot <slot> serve as content distribution outlets in component templates. <slot> itself will be replaced. details (opens new window)
teleport Move part of the template somewhere else in the DOM, outside of the Vue app. details (opens new window) x

# Reactivity API

# Basic Reactivity APIs

Basic Reactivity APIs description H5 App
reactive Returns a reactive copy of the object.details (opens new window)
readonly Takes an object (reactive or plain) or a ref and returns a readonly proxy to the original. details (opens new window)
isProxy Checks if an object is a proxy created by reactive or readonly.details (opens new window)
isReactive Checks if an object is a reactive proxy created by reactive.details (opens new window)
isReadonly Checks if an object is a readonly proxy created by readonly.details (opens new window)
toRaw Returns the raw, original object of a reactive or readonly proxy. details (opens new window)
markRaw Marks an object so that it will never be converted to a proxy. Returns the object itself.details (opens new window)
shallowReactive Creates a reactive proxy that tracks reactivity of its own properties but does not perform deep reactive conversion of nested objects (exposes raw values).details (opens new window)
shallowReadonly Creates a proxy that makes its own properties readonly, but does not perform deep readonly conversion of nested objects (exposes raw values).details (opens new window)

# Refs

Refs description H5 App
ref Takes an inner value and returns a reactive and mutable ref object. The ref object has a single property .value that points to the inner value.details (opens new window)
unref Returns the inner value if the argument is a ref, otherwise return the argument itself. This is a sugar function for val = isRef(val) ? val.value : val.details (opens new window)
toRef Can be used to create a ref for a property on a source reactive object. The ref can then be passed around, retaining the reactive connection to its source property.details (opens new window)
toRefs Converts a reactive object to a plain object where each property of the resulting object is a ref pointing to the corresponding property of the original object.details (opens new window)
isRef Checks if a value is a ref object.details (opens new window)
customRef Creates a customized ref with explicit control over its dependency tracking and updates triggering.details (opens new window)
shallowRef Creates a ref that tracks its own .value mutation but doesn't make its value reactive.details (opens new window)
triggerRef Execute any effects tied to a shallowRef manually.details (opens new window)

# Computed and watch

Computed and watch description H5 App
computed Takes a getter function and returns an immutable reactive ref object for the returned value from the getter.details (opens new window)
watchEffect Runs a function immediately while reactively tracking its dependencies and re-runs it whenever the dependencies are changed.details (opens new window)
watch The watch API is the exact equivalent of the Options API this.$watch (and the corresponding watch option). watch requires watching a specific data source and applies side effects in a separate callback function.details (opens new window)

# Composition API

Composition API description H5 App
setup A component option that is executed before the component is created, once the props are resolved. It serves as the entry point for composition APIs.details (opens new window)
Lifecycle Hooks Lifecycle hooks can be registered with directly-imported onX functions.details (opens new window)
Provide / Inject provide and inject enables dependency injection. Both can only be called during setup() with a current active instance.details (opens new window)
getCurrentInstance getCurrentInstance enables access to an internal component instance.details (opens new window)

# Global variable

The way to implement global variables needs to follow the development specifications of Vue single file mode.

# Common problem

# 1. How to get the data passed on the previous page

Obtained in onLoad, the parameter of onLoad is the data passed by other pages to open the current page.

# 2. How to set global data and global methods

uni-app has built-in Vuex (opens new window). For its use in app, please refer to hello-uniapp store/index.js.

	//store.js
	import {createStore} from 'vuex'
	const store = createStore({
		state: {...},
		mutations: {...},
		actions: {...}
	})
	export default store

	//main.js
	import App from './App'
	import {createSSRApp} from 'vue'
	import store from './store'
	export function createApp() {
		const app = createSSRApp(App)
		app.use(store)
		return {
			app
		}
	}

	//test.vue When using:
	import {mapState,mapMutations} from 'vuex'

# 3. How to catch onError of app

Since onError is not a complete life cycle, only a method of catching errors is provided, and a callback function named onError can be added to the root component of the app. as follows:

	export default {
		// Only apps have an onLaunch lifecycle
		onLaunch () {
		   // ...
		},

		// capture app error
		onError (err) {
		   console.log(err)
		}
	}

# 4. Component property settings do not take effect

When some properties are repeatedly set to the same value, they are not synchronized to the View layer. For example, every time you set the scroll-top property of a scroll-view component to 0, it only gets back to the top the first time. This is due to the props unidirectional data flow feature. When the actual value of scroll top inside the component changes, the binding properties do not change with it.

There are two solutions (take the scroll-view component as an example):

  1. Monitor the scroll event, record the value of the internal change of the component, and set the current value of the record before setting the new value
	<scroll-view scroll-y="true" :scroll-top="scrollTop" @scroll="scroll"></scroll-view>
export default {
    data() {
        return {
            scrollTop: 0,
            old: {
                scrollTop: 0
            }
        }
    },
    methods: {
        scroll: function(e) {
            this.old.scrollTop = e.detail.scrollTop
        },
        goTop: function(e) {
            this.scrollTop = this.old.scrollTop
            this.$nextTick(function() {
                this.scrollTop = 0
            });
        }
    }
}

  1. Monitor the scroll event, get the value of the internal change of the component, and update its binding value in real time
	<scroll-view scroll-y="true" :scroll-top="scrollTop" @scroll="scroll"></scroll-view>
	export default {
		data() {
			return {
				scrollTop: 0,
			}
		},
		methods: {
			scroll: function(e) {
				// If you use this method, please add debounce by yourself
				this.scrollTop = e.detail.scrollTop
			},
			goTop: function(e) {
				this.scrollTop = 0
			}
		}
	}

The second solution may cause jitter in some components, and the first solution is recommended .