This article mainly introduces the three important functions App()getApp()Page() in the WeChat applet, as well as the initialization of the page , data modularization function.
##App()
App() function is used to register a small program. Accepts an object parameter, which specifies the life cycle function of the applet, etc.
object parameter description:
Type | Description | Trigger timing | |
Function | Life cycle function--listen to the initialization of the applet | When the initialization of the applet is completed, onLaunch will be triggered (only triggered once globally) | |
Function | Life cycle function--listening to the mini program display | When the mini program starts, or enters the foreground display from the background, onShow | will be triggered |
Function | Life cycle function--listening to the mini program hiding | When the mini program enters the background from the foreground, onHide will be triggered | |
Any | Developers can add any function or data to the Object parameter, which can be accessed using this |
Description | data | |
Page Initial data | onLoad | |
Life cycle function--listen to page loading | onReady | |
Life cycle function--listen to the completion of page rendering | onShow | |
Life cycle Function--Listening page display | onHide | |
Life cycle function--Listening page hiding | onUnload | |
Life cycle function--listen for page unloading | Others | |
Developers can add any function or data to the Object parameter, which can be accessed using this |
<view>{{text}}</view> <view>{{array[0].msg}}</view> Page({ data: { text: 'init data', array: [{msg: '1'}, {msg: '2'}] } }) Copy after login 复制代码 <view bindtap="viewTap"> click me </view> Page({ viewTap: function() { console.log('view tap') } }) Copy after login 复制代码
setData()参数格式 <!--index.wxml--> <view>{{text}}</view> <button bindtap="changeText"> Change normal data </button> <view>{{array[0].text}}</view> <button bindtap="changeItemInArray"> Change Array data </button> <view>{{obj.text}}</view> <button bindtap="changeItemInObject"> Change Object data </button> <view>{{newField.text}}</view> <button bindtap="addNewField"> Add new data </button> //index.js Page({ data: { text: 'init data', array: [{text: 'init data'}], object: { text: 'init data' } }, changeText: function() { // this.data.text = 'changed data' // bad, it can not work this.setData({ text: 'changed data' }) }, changeItemInArray: function() { // you can use this way to modify a danamic data path var changedData = {} var index = 0 changedData['array[' + index + '].text'] = 'changed data' this.setData(changedData) }, changeItemInObject: function(){ this.setData({ 'object.text': 'changed data' }); }, addNewField: function() { this.setData({ 'newField.text': 'new data' }) } }) Copy after login 复制代码 var common = require('common.js') Page({ helloMINA: function() { common.sayHello('MINA') } }) Copy after login 复制代码下一篇我们会介绍视图层的WXML。 更多微信小程序之代码解析:二.逻辑层 相关文章请关注PHP中文网! |