Skip to main content
Version: Next

Entry Component

Every Taro application needs an entry component (Vue component) to register the application. The portal file is by default app.js in the src directory.

In the entry component we can set the global state or access the lifecycle of the mini program entry instance.

Note:

  1. Since Taro v3.1, the Vue2 entry component has been written with Breaking Changes, see 3.1.0 Release Information for details.
  2. Due to a change in the Vue3 Global API (0009-global-api-change), the Vue3's entry component is written differently than Vue2.

Example Code

app.js
import Vue from 'vue'
// Assuming we have configured the vuex in '. /store' is configured with vuex
import store from './store'

const App = {
store,

// All Vue lifecycle methods can be used
mounted () {},

// Corresponds to onLaunch
onLaunch () {},

// Corresponds to onShow
onShow (options) {},

// Corresponds to onHide
onHide () {},

render(h) {
// this.$slots.default is the page that will be rendered
return h('block', this.$slots.default)
}
}

export default App

Entry Component Configuration

Please refer to the global configuration

Lifecycle

In Vue2 and Vue3, the additional lifecycle methods added by Taro are written in the same way

In addition to supporting Vue's lifecycle methods, the entry component additionally supports the following lifecycles according to the mini program's standards.

onLaunch (options)

onLaunch for the corresponding app in the mini program environment.

The program initialization parameters are accessible through getCurrentInstance().router.params during this lifecycle

Parameters

options
ProertyTypeDescription
pathstringPath for launch mini program
scenenumberScene values for launch mini program
queryObjectParameters for launch mini program
shareTicketstringshareTicket,See Get More Forwarding Information
referrerInfoObjectSource information. Source information. Returned when accessing an mini program from another mini program, public number or app. Otherwise returns {}
options.referrerInfo
options.referrerInfo
ProertyTypeDescription
appIdstringSource mini program, or public number (in WeChat))
extraDataObjectThe data passed from the source mini program is supported by WeChat and Baidu smart program at scene=1037 or 1038
sourceServiceIdstringSource plugin, visible when in plugin run mode

The options parameter may vary from field to field in different mini program

Scene values , there are differences in WeChat mini program and Baidu smart program, please refer to them respectively Wechat mini programBaidu smart program Documents

onShow (options)

Triggered when the page is displayed/cut to the foreground

As with the onLaunch lifecycle, program initialization parameters can be accessed during this lifecycle by accessing the options parameter or calling getCurrentInstance().router.

The parameters are basically the same as those obtained in onLaunch, but with two additional parameters in Baidu Smart program as follows.

Property
entryType
appURL

onHide ()

Triggered when the program cuts the background.

onPageNotFound (Object)

Triggered when the page to be opened by the program does not exist.

Parameters

Object
ProertyTypeDescription
pathstringPath to non-existent page
queryObjectOpen the query parameter of a non-existent page
isEntryPagebooleanWhether the first page of this launch (e.g. coming in from a portal such as sharing, the first page is the developer-configured sharing page)