May 17, 2021 WeChat Mini Program Development Document
In the last chapter, we quickly created a QuickStart project with developer tools. You can notice that different types of files have been generated in this project:
Let's look at the role of these four files separately.
JSON is a data format, not a programming language, and in small programs, JSON plays the role of static configuration.
We can see that there is an app.json and project.config.json at the root of the project, and a logs.json in the pages/logs directory, so let's explain their purpose in turn.
app.json is the global configuration of the current gadget, including all page paths, interface performance, network timeouts, bottom tab, and so on. The app.json configuration in the QuickStart project is as follows:
{
"pages":[
"pages/index/index",
"pages/logs/logs"
],
"window":{
"backgroundTextStyle":"light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "WeChat",
"navigationBarTextStyle":"black"
}
}
Let's briefly say what this configuration means for each item:
Additional configuration item details can be found in the documentation for the configuration app.json of the program.
Usually everyone will do some personalized configurations for their respective preferences, such as interface color, compilation, and more, when you change another computer reinstall tool, you have to reconfigure.
Taking into account this, the applet developer tool generates a project.config.json at the root of each project, and any configuration you do on the tool will write to this file, when you reinstall the tool or change your computer.When you load the code package of the same project, the developer tool will automatically resume to restore the personalized configuration when you develop projects, which will include the color of the editor, automatic compression, etc., etc.Options.
Other configuration item details can refer to the documentation Developer tool configuration 。
The page.json here is actually used to represent the Logs.json under the Pages / Logs directory.
If your entire applet is blue, you can declare the top color in app.json.The actual situation may not be the case, maybe you have different hue to distinguish between different functional modules in your small program, so we offer Page.json to let developers can independently define some properties of each page, such as just justSay the top color, whether the drop-down refresh is allowed.
Other configuration item details can refer to the documentation Page configuration 。
Here you say some of the precautions for JSON configuration in the small program.
The JSON file is wrapped in a braklet {}, express data by key-value.JSON's key must be wrapped in a double quotation. In practice, when writing JSON, I forgot to add double quotes to the key value or write double quotes to a single quotation mark is a common error.
JSON's value can only be the following data formats, and any other format trigger a message, such as undefined in JavaScript.
It is also important to note that comments cannot be used in JSON files, and attempting to add comments will result in an error.
Web programming people know that web programming uses a combination of HTML and CSS and JS, where HTML is used to describe the current structure of the page, CSS is used to describe what the page looks like, and JS is usually used to handle the interaction between the page and the user.
By the same toe, there are similar roles in small programs, where WXML acts as an HTML-like role. Open pages/index/index.wxml and you'll see the following:
<view class="container">
<view class="userinfo">
<button wx:if="{{!hasUserInfo && canIUse}}"> 获取头像昵称 </button>
<block wx:else>
<image src="{{userInfo.avatarUrl}}" background-size="cover"></image>
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
</block>
</view>
<view class="usermotto">
<text class="user-motto">{{motto}}</text>
</view>
</view>
Much like HTML, WXML consists of tags, properties, and so on. But there are many different things, let's explain one by one:
More detailed documentation can refer to WXML
WXSS has most of the features of CSS, and small programs have been expanded and modified in WXSS.
More detailed documentation can be referred to WXSS.
It is not enough for a service to display only the interface, but also to interact with the user: respond to the user's clicks, get the user's location, and so on. In the small program, we process the user's actions by writing a JS script file.
<view>{{ msg }}</view>
<button bindtap="clickMe">点击我</button>
When we clicked the button button, we wanted to display msg on the interface as "Hello World", so we declared a property on the button: bindtap, which stated the clickMe method in the JS file in response to this click:
Page({
clickMe: function() {
this.setData({ msg: "Hello World" })
}
})
Responding to a user's actions is as simple as a more detailed event that can be referenced in document WXML - Events.
You can also call the rich APIs provided by small programs in JS, which make it easy to tune the capabilities provided by WeChat, such as getting user information, local storage, WeChat payments, and more. I n the QuickStart example above, wx.getUserInfo is called in pages/index/index.js to get the profile picture and nickname of the WeChat user, and finally displays the information obtained on the interface via setData. More APIs can refer to the API of the documentation gadget.
Through this section, you learn about the types of files involved in small programs and the corresponding roles, and in the next chapter, we'll give "strings" of the files involved in this chapter through the "framework of small programs" to get them all to work.