WeChat public platform mini-program development tutorial

Source: Internet
Author: User
This document will take you step by step to create a small program, and you can experience the actual effect of the small program on your phone. The homepage of this applet displays the welcome speech and the current user's profile picture. click the profile picture to view the startup log of the current applet on the new page. This document will take you step by step to create a small program, and you can experience the actual effect of the small program on your phone. The homepage of this applet displays the welcome speech and the current user's profile picture. click the profile picture to view the startup log of the current applet on the new page.

1. obtain the AppID of the applet

If you are an invited developer, we will provide an account, using the provided account, login to the https://mp.weixin.qq.com, you can in the site "settings"-"developer settings, check the AppID of the applet. Note that you cannot directly use the AppID of the service number or subscription number.

Skip this step if the visitor mode is used.

After the project is successfully created, you can click the project to go to the complete developer tool interface. click the left-side navigation bar to view and edit our code in "edit, in "debugging", you can test the code and simulate the effect of the applet on the client. in "project", you can send the code to your mobile phone to preview the actual effect.

3. write code to create a small program instance

Click "edit" in the left-side navigation bar of the developer tool. we can see that this project has been initialized and contains some simple code files. The key and necessity are app. js, app. json, and app. wxss. Where,.jsThe suffix is a script file,.jsonThe suffix file is the configuration file,.wxssThe suffix is the style sheet file. The applet will read these files and generate the applet instance.

Next we will briefly understand the functions of these three files to facilitate modification and develop our own mini programs from scratch.

App. js is the script code of a small program. In this file, we can listen to and process the lifecycle functions of the applet and declare global variables. Call the rich APIs provided by the framework, such as synchronous storage and synchronous reading of local data. For more information about available APIs, see the API documentation.

// App. jsApp ({onLaunch: function () {// call the API to obtain data from the local cache var logs = wx. getStorageSync ('logs') | [] logs. unshift (Date. now () wx. setStorageSync ('logs', logs)}, getUserInfo: function (cb) {var that = this; if (this. globalData. userInfo) {typeof cb = "function" & cb (this. globalData. userInfo)} else {// call the logon interface wx. login ({success: function () {wx. getUserInfo ({success: function (res) {that. globalData. userInfo = res. userInfo; typeof cb = "function" & cb (that. globalData. userInfo) }}) }};}, globalData: {userInfo: null }})

App. json is a global configuration for the entire applet. In this file, we can configure the page that the applet consists of, configure the background color of the applet window, configure the navigation bar style, and configure the default title. Note that you cannot add any comments to this file. For more configuration items, refer to configuration details.

{  "pages":[    "pages/index/index",    "pages/logs/logs"  ],  "window":{    "backgroundTextStyle":"light",    "navigationBarBackgroundColor": "#fff",    "navigationBarTitleText": "WeChat",    "navigationBarTextStyle":"black"  }}

App. wxss is the public style table of the entire applet. We can directly use the style rules declared in app. wxss on the class attribute of the page component.

/**app.wxss**/.container {  height: 100%;  display: flex;  flex-direction: column;  align-items: center;  justify-content: space-between;  padding: 200rpx 0;  box-sizing: border-box;}
Create Page

In this tutorial, we have two pages: the index page and the logs page, that is, the welcome page and the display page of the applet startup log. they are all under the pages directory. The path + page name of each page in the applet must be written in pages in app. json, and the first page in pages is the homepage of the applet.

Each applet page consists of four different suffix files with the same name in the same path, such as index. js, index. wxml, index. wxss, and index. json..jsThe suffix file is a script file,.jsonThe suffix file is the configuration file,.wxssThe suffix is a style sheet file,.wxmlA file with a suffix is a page structure file.

Index. wxml is the structure file of the page:

 
   
      
       
   
    {{userInfo.nickName}}
     
    
      
   
    {{motto}}
     
  
 

In this example ,, To build a page structure and bind data and interactive processing functions.

Index. js is a page script file. in this file, we can listen to and process page Lifecycle Functions, obtain applet instances, declare and process data, and respond to page interaction events.

// Index. js // Obtain the application instance var app = getApp () Page ({data: {motto: 'Hello World', userInfo: {}}, // bindViewTap: function () {wx. navigateTo ({url :'.. /logs '})}, onLoad: function () {console. log ('onload') var that = this // call the method of the application instance to obtain the global data app. getUserInfo (function (userInfo) {// update the data that. setData ({userInfo: userInfo })})}})

Index. wxss is the style sheet of the page:

/**index.wxss**/.userinfo {  display: flex;  flex-direction: column;  align-items: center;}.userinfo-avatar {  width: 128rpx;  height: 128rpx;  margin: 20rpx;  border-radius: 50%;}.userinfo-nickname {  color: #aaa;}.usermotto {  margin-top: 200px;}

The style sheet of the page is unnecessary. When a page style sheet exists, the style rules in the style sheet will overwrite the style rules in app. wxss. If you do not specify a style sheet for a page, you can also directly use the style rules specified in app. wxss in the structure file of the page.

Index. json is the page configuration file:

The page configuration file is unnecessary. When there is a page configuration file, the configuration item will overwrite the same configuration item in the window of app. json on this page. If no page configuration file is specified, the default configuration in app. json is directly used on the page.

Logs page structure

 
   
      
   
    {{index + 1}}. {{log}}
     
  
 

Logs page usage Control tags to organize code. Use onwx:for-itemsBindlogsData, andlogsExpand nodes in a data loop

//logs.jsvar util = require('../../utils/util.js')Page({  data: {    logs: []  },  onLoad: function () {    this.setData({      logs: (wx.getStorageSync('logs') || []).map(function (log) {        return util.formatTime(new Date(log))      })    })  }})

The running result is as follows:

For more articles on the public platform mini-program development tutorial, please follow the PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.