The mini-programs have quickly become the focus of discussion before they are officially released. you may feel that you can experience the development process of the mini-programs only when you receive an internal test invitation, let's take a look at the following ....
The mini-programs have quickly become the focus of discussion before they are officially released. you may feel that you can experience the development process of the mini-programs only when you receive an internal test invitation, let's take a look at the following.
Download Web Developer Tools
First, we provide our own integrated development tool for small programs. you only need to download it on this page:
After the download is complete, open the developer tool and there will be a QR code logon interface. You can log in with your scan code. then, the developer tool will help us create a default project. the file structure of the project is as follows:
All code editing and run preview can be performed in this developer tool. Next let's take a look at the project structure of the applet.
Project structure
For example, the root directory contains three files: app. js, app. json, and app. wxss. App. js is the script file at the main entrance of the program, app. json is the global configuration file, and app. wxss is the style sheet file of the applet.
Let's take a look at app. json:
{ "pages":[ "pages/index/index", "pages/logs/logs" ], "window":{ "backgroundTextStyle":"light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "WeChat", "navigationBarTextStyle":"black" }}
The configuration file defines two nodes. pages are the paths corresponding to all pages of the applet, and window is the configuration information of the applet window.
Let's take a look at the style file app. wxss:
.container { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: space-between; padding: 200rpx 0; box-sizing: border-box;}
We don't need to go into the details of the specific style, just need to understand the project structure first. Next let's take a look at app. js, the main portal of the program:
// App. js App ({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 }})
Here, an App object is initialized and three methods onLaunch, getUserInfo and globalData are defined. Let's take a look at onLaunch:
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 )}
First, the wx. getStorageSync method obtains a local cached data with the logs as the key. The logs passed in this method does not have any special meaning, but is used to indicate the cache data we use. This mechanism is similar to nsuserults ULTS in iOS.
Then, we want to insert the current Date logs. unshift (Date. now () into the cache array ()). Finally, call the setStorageSync method to write our new cache content to the local cache.
Because the onLaunch method is the life cycle method of the applet, it is called when the applet starts, and the current start date is recorded and written to the local cache. That's right, the onLaunch method does this.
Let's take a look at the getUserInfo method. it obtains the logon information of the current user by calling functions on the wx. login and wx. getUserInfo platforms, and transmits the information to the callback function cb:
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 )}})}})}}
As for the first if judgment if (this. globalData. userInfo), we do not need to go into it for the time being, just look at the else part.
Page structure
After learning about the several files in the root directory, let's take a look at the page files, as shown in the project structure at the beginning:
All pages are in the pages folder. In this example, there are two page indexes and logs. Remember the page configuration we saw in app. json:
"pages":[ "pages/index/index", "pages/logs/logs" ]
Exactly corresponds to the two directories we see now. remember that the first element in the pages array serves as the homepage of our applet. Remember, the index page is the homepage because it is the first element in pages, not because its name is index.
Let's take a look at the structure of the index page, index. js, index. wxml, index. wxss. Index. js is the script file of the page, index. wxml is the UI file of the page, and index. wxss is the style file of the page.
First look at index. js:
// 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 })})}})
GetApp () method to get our app instance. Then, let's look at the onLoad method, use the getUserInfo method we just mentioned to get user information, and set it to the data attribute.
The bindViewTap method binds an event, which calls the wx. navigateTo method. This method is actually page jump. it is not difficult to see from the code that it jumps to the logs page.
This is the content of the script file. let's continue to look at the UI file, index. wxml:
This is the UI file of the index page of the applet. In fact, the platform defines a series of components, and the outermost layer is Do you still remember container? We define the style of the app. wxss in the outermost layer. It contains two views. Let's take a look at the first one:
First bindtap = "bindViewTap" binds a click event to this View, that is, the method corresponding to index. js in front of us. you can click this View to jump to the logs page. The View contains an Image and Text. The src attribute of the Image is set to userInfo. avatarUrl, which indicates the current user's profile. userInfo. nickName is used in the Text to represent the nickName of the current user.
In this way, the overall logic of the index page is complete, and there is an index. wxss style file, which we will skip first.
Let's take a look at the second view:
Motto is actually an attribute defined in index. js:
data: { motto: 'Hello World', userInfo: {} }
It will display a Hello World on the page.
Now, we can switch to the debugging interface to see the home page of the applet, which is exactly the same as the UI we just described:
Because my environment does not have an internal test account, the project is not associated with AppID in the upload area. if you have a test account, your AppID is displayed. Currently, only the internal test account can Upload applets. This is the only difference. You cannot upload files without an internal test account, but you can develop and test files locally.
This Upload method of applets may make you feel a little different. Generally, you need to build and maintain your own Web applications. This hosting method of small programs is already similar to developing a native App. Although the front-end uses JavaScript technology that looks like web, its core idea is different from that of traditional web apps. It is more like a React Native implementation.
End
This time, together with everyone, we have a complete experience of the overall structure and development ideas of the simplest small program from start to end. I personally think that if we can find a suitable starting point, the platform for applets can still find some good opportunities. However, in my opinion, do not think that the appearance of a mini-program will immediately subvert anyone, or you do not need to hear people on the Internet say that it is difficult to become a major event and feel that it has no chance. Find what you are good at and what users need, maybe they will create some good products. This is also a basic technical review for everyone, hoping to help everyone.
The above is the detailed content that will take you through the complete development process of the mini-program. For more information, see other related articles in the first PHP community!