Core framework functions for creating WeChat Mini Program applications, pages, and components from Vue.js component options. These functions handle the conversion between Vue.js component architecture and WeChat Mini Program's component model.
Creates a uni-app application instance configured for WeChat Mini Program deployment.
/**
* Creates a uni-app application instance for WeChat Mini Program
* @param options - Vue.js app options including lifecycle hooks and global data
* @returns Vue instance configured for WeChat Mini Program
*/
function createApp(options: AppOptions): VueInstance;Usage Example:
import { createApp } from "@dcloudio/uni-mp-weixin";
const app = createApp({
onLaunch(options) {
console.log('App launched with options:', options);
// Initialize app data
this.globalData.userInfo = null;
},
onShow(options) {
console.log('App shown');
},
onHide() {
console.log('App hidden');
},
onError(error) {
console.error('App error:', error);
},
globalData: {
userInfo: null,
settings: {}
}
});Creates a WeChat Mini Program page from Vue.js component options, handling lifecycle mapping and event binding.
/**
* Creates a WeChat Mini Program page from Vue component options
* @param options - Vue.js page component options with WeChat Mini Program lifecycle hooks
* @returns WeChat Mini Program page component
*/
function createPage(options: PageOptions): WeXinPageComponent;Usage Example:
import { createPage } from "@dcloudio/uni-mp-weixin";
const indexPage = createPage({
data() {
return {
title: 'Welcome',
userList: [],
loading: false
};
},
onLoad(query) {
console.log('Page loaded with query:', query);
this.loadData();
},
onShow() {
console.log('Page shown');
},
onPullDownRefresh() {
this.refreshData();
},
onReachBottom() {
this.loadMoreData();
},
onShareAppMessage() {
return {
title: this.title,
path: '/pages/index/index'
};
},
methods: {
loadData() {
this.loading = true;
// Load page data
uni.request({
url: '/api/users',
success: (res) => {
this.userList = res.data;
},
complete: () => {
this.loading = false;
}
});
},
refreshData() {
this.loadData();
setTimeout(() => {
uni.stopPullDownRefresh();
}, 1000);
},
loadMoreData() {
// Load additional data
}
}
});Creates a WeChat Mini Program component from Vue.js component options with proper lifecycle mapping.
/**
* Creates a WeChat Mini Program component from Vue component options
* @param options - Vue.js component options with props, methods, and lifecycle hooks
* @returns WeChat Mini Program component
*/
function createComponent(options: ComponentOptions): WeixinComponent;Usage Example:
import { createComponent } from "@dcloudio/uni-mp-weixin";
const customButton = createComponent({
props: {
text: {
type: String,
default: 'Click me'
},
type: {
type: String,
default: 'primary'
},
disabled: {
type: Boolean,
default: false
}
},
data() {
return {
pressed: false
};
},
created() {
console.log('Component created');
},
attached() {
console.log('Component attached');
},
ready() {
console.log('Component ready');
},
methods: {
handleTap() {
if (this.disabled) return;
this.pressed = true;
this.$emit('tap', {
type: this.type,
text: this.text
});
setTimeout(() => {
this.pressed = false;
}, 150);
}
}
});Creates a subpackage application instance that integrates with the main app's global data and lifecycle.
/**
* Creates a subpackage app instance for WeChat Mini Program
* @param vm - Vue instance for the subpackage
* @returns Vue instance configured for subpackage deployment
*/
function createSubpackageApp(vm: VueInstance): VueInstance;Creates a WeChat Mini Program plugin from a Vue.js instance with plugin-specific lifecycle handling.
/**
* Creates a WeChat Mini Program plugin from Vue instance
* @param vm - Vue instance configured as a plugin
* @returns Vue instance configured for plugin deployment
*/
function createPlugin(vm: VueInstance): VueInstance;// Application configuration options
interface AppOptions {
/** Called when the app is launched */
onLaunch?: (options: LaunchOptions) => void;
/** Called when the app becomes visible */
onShow?: (options: ShowOptions) => void;
/** Called when the app is hidden */
onHide?: () => void;
/** Called when an error occurs */
onError?: (error: string) => void;
/** Called when a page is not found */
onPageNotFound?: (options: PageNotFoundOptions) => void;
/** Called when system theme changes */
onThemeChange?: (options: ThemeChangeOptions) => void;
/** Called when there's an unhandled promise rejection */
onUnhandledRejection?: (options: UnhandledRejectionOptions) => void;
/** Global data accessible throughout the app */
globalData?: Record<string, any>;
/** Custom methods and properties */
[key: string]: any;
}
// Page component configuration options
interface PageOptions {
/** Page data function returning reactive data */
data?: () => Record<string, any>;
/** Called when page loads with query parameters */
onLoad?: (query: Record<string, string>) => void;
/** Called when page becomes visible */
onShow?: () => void;
/** Called when page initial render is complete */
onReady?: () => void;
/** Called when page is hidden */
onHide?: () => void;
/** Called when page is unloaded */
onUnload?: () => void;
/** Called when user pulls down to refresh */
onPullDownRefresh?: () => void;
/** Called when user reaches bottom of page */
onReachBottom?: () => void;
/** Called when user shares the page */
onShareAppMessage?: (options: ShareOptions) => ShareResult;
/** Called when page scrolls */
onPageScroll?: (options: PageScrollOptions) => void;
/** Called when window resizes */
onResize?: (options: ResizeOptions) => void;
/** Called when tab item is tapped */
onTabItemTap?: (item: TabItemOptions) => void;
/** Custom methods */
methods?: Record<string, Function>;
/** Custom properties */
[key: string]: any;
}
// Component configuration options
interface ComponentOptions {
/** Component data function */
data?: () => Record<string, any>;
/** Component properties definition */
props?: Record<string, PropDefinition>;
/** Component methods */
methods?: Record<string, Function>;
/** Called when component instance is created */
created?: () => void;
/** Called when component is attached to DOM */
attached?: () => void;
/** Called when component is ready */
ready?: () => void;
/** Called when component is moved */
moved?: () => void;
/** Called when component is detached */
detached?: () => void;
/** Custom properties */
[key: string]: any;
}
// Property definition for components
interface PropDefinition {
type?: Function | Function[];
default?: any | (() => any);
required?: boolean;
validator?: (value: any) => boolean;
}
// Launch options
interface LaunchOptions {
path: string;
query: Record<string, string>;
scene: number;
shareTicket?: string;
referrerInfo?: ReferrerInfo;
}
// Share result configuration
interface ShareResult {
title?: string;
path?: string;
imageUrl?: string;
}