Skip to main content
The Page API wraps the native Page() constructor to add MorJS lifecycle hooks, mixin composition, appLifetimes support, $eventListener binding, and cross-platform adapter invocation.

Functions

createPage

Registers a page with the MorJS runtime. Internally calls enhancePage and then the native Page() constructor.
function createPage<D extends IData, T extends IData>(
  options: MorPageOptions<D, T>,
  sourceType?: SOURCE_TYPE
): any
options
MorPageOptions<D, T>
required
The page configuration object. Supports both Alipay and WeChat page option shapes, plus MorJS extensions (mixins, $eventListener, appLifetimes).
sourceType
SOURCE_TYPE
Specifies the DSL source type (SOURCE_TYPE.ALIPAY or SOURCE_TYPE.WECHAT). When omitted MorJS logs a warning because lifecycle handling differs between platforms.
import { createPage } from '@morjs/core'

createPage({
  data: {
    title: 'Hello'
  },
  onLoad(query) {
    console.log('page loaded', query)
  },
  onReady() {
    console.log('page ready')
  },
  onUnload() {
    console.log('page unloaded')
  }
})

aPage

Registers an Alipay-style page. Equivalent to createPage(options, SOURCE_TYPE.ALIPAY).
function aPage<D extends IData, T extends IData>(
  options: MorPageOptions<D, T>
): any
import { aPage } from '@morjs/core'

aPage({
  data: { count: 0 },
  onLoad() {
    this.setData({ count: 1 })
  }
})

wPage

Registers a WeChat-style page. Equivalent to createPage(options, SOURCE_TYPE.WECHAT).
function wPage<D extends IData, T extends IData>(
  options: MorPageOptions<D, T>
): any
import { wPage } from '@morjs/core'

wPage({
  data: { count: 0 },
  onLoad() {
    this.setData({ count: 1 })
  }
})

enhancePage

Applies all MorJS enhancements to a page options object without registering it. Useful when you need the enhanced options for further processing (for example, PageToComponent).
function enhancePage<D extends IData, T extends IData>(
  options: MorPageOptions<D, T>,
  sourceType?: SOURCE_TYPE
): IData
options
MorPageOptions<D, T>
required
The page options to enhance.
sourceType
SOURCE_TYPE
Source DSL type. Affects how onResize is handled (Alipay puts it inside events, WeChat uses a top-level method).
return
IData
The mutated page options object with all lifecycle hooks wired and mixins merged.

registerPageAdapters

Registers one or more page-level cross-platform adapters.
function registerPageAdapters(
  adapters?: MorPageAdapter | MorPageAdapter[]
): void
adapters
MorPageAdapter | MorPageAdapter[]
Adapter(s) whose initPage method will be called for every page created by createPage.
import { registerPageAdapters } from '@morjs/core'

registerPageAdapters({
  initPage(pageOptions) {
    // add a custom method to every page
    pageOptions.myHelper = function () {}
  }
})

Types

MorPageOptions

The complete page options type accepted by createPage, aPage, and wPage.
type MorPageOptions<D extends IData, T extends IData> = {
  mixins?: CompoundPageOptions<D, T>[]
  $eventListener?: Record<string, IAnyFunc>
} & CompoundPageOptions<D, T> &
  ThisType<WechatMiniprogram.Page.Instance<D, T> & tinyapp.IPageInstance<D>>
mixins
CompoundPageOptions<D, T>[]
Array of mixin objects. Lifecycle methods are merged and called in order; non-lifecycle methods use the last-declared version; data and object fields are shallow-merged.
$eventListener
Record<string, IAnyFunc>
Map of global event names to handler functions. MorJS registers these on the shared $event emitter when the page loads and removes them on unload.
appLifetimes
{ show?: Function; hide?: Function }
Handlers that fire when the host app shows or hides, while this page is active. Registered in onLoad and unregistered in onUnload.

MorPageAdapter

interface MorPageAdapter {
  initPage?: (pageOptions: Record<string, any>) => void
}
initPage
(pageOptions: Record<string, any>) => void
Called once per page registration, after all enhancements are applied but before the native Page() constructor runs.

Lifecycle hooks

The following page lifecycle methods are intercepted by MorJS:
MethodMorHookNotes
onLoadpageOnLoadAlso registers $eventListener and appLifetimes
onReadypageOnReadyEmits $mor:pageOnReady:<flag>
onShowpageOnShowEmits $mor:pageOnShow:<flag>
onHidepageOnHideEmits $mor:pageOnHide:<flag>
onUnloadpageOnUnloadRemoves event listeners and appLifetimes
onResizeEmits $mor:pageOnResize:<flag>; Alipay uses events.onResize
The pageOnConstruct hook (falling back to pageOnInit) fires at the start of enhancePage.

Alipay-specific lifecycle methods (via mixins)

onTitleClick, onOptionMenuClick, onPopMenuClick, onPullIntercept, onTabItemTap

WeChat-specific lifecycle methods (via mixins)

onShareTimeline, onResize, onAddToFavorites

Build docs developers (and LLMs) love