Mini Program Code Structure

View Markdown

Last edited on 2026-11-12

In the previous chapter, we quickly created a QuickStart project using the developer tools. You may have noticed that various types of files were generated within this project:

  1. 1.JSON configuration files with a .json extension
  2. 2.WXML template files with a .wxml extension
  3. 3.WXSS style files with a .wxss extension
  4. 4.JS script logic files with a .js extension

Next, we will examine the functions of these four types of files one by one.

JSON Configuration

JSON is a data format, not a programming language. In Mini Program, it serves as a static configuration element.

There are app.json and project.config.json in the root directory of the project. Additionally, there is a logs.json file in the pages/logs directory. Let’s explain their purposes respectively.

Mini Program Configuring app.json

app.json contains the global configuration for the current Mini Program, including all page paths, interface layout, network timeout settings, bottom tabs, and more.

The app.json file in the QuickStart project has quite a few configurations, including fields such as pages and window. Here is a simplified version:

{
 "pages": [
 "pages/index/index",
 "pages/logs/logs"
],
 "window": {
 "navigationBarTextStyle": "black",
 "navigationBarTitleText": "Weixin",
 "navigationBarBackgroundColor": "#ffffff"
}
}
  1. 1.The pages field — used to specify all page paths in the current Mini Program. This helps the WeChat client determine which directory contains the pages of your Mini Program. The first entry represents the home page of Mini Program (the first page displayed when opening it).
  2. 2.The window field — defines the background color of the top bar and text colors for all pages in Mini Program.

For details on other configuration options, refer to the documentation Configuring app.json in Mini Program.

Tool Configuration via project.config.json

When using any tool, users often customize it according to their preferences, such as adjusting interface colors or compilation settings. When reinstalling the tool on another computer, these customizations need to be reconfigured.

To address this issue, the Mini Program developer tools generate a project.config.json file in the root directory of each project. Any configurations you set in the tools are saved in this file. When you reinstall the tools or switch to another computer, simply load the same project code package, and the developer tools will automatically restore your customized settings, including editor colors and automatic compression during code upload, among other options.

For more details on other configuration options, refer to the documentation Configuring Developer Tools.

Page Configuration via page.json

Here, page.json is actually used to represent page-related configurations such as logs.json in the pages/logs directory.

If your entire Mini Program adopts a blue theme, you can specify the top bar color as blue in app.json. However, in practice, each page in Mini Program might have a different color scheme to distinguish functional modules. That’s why page.json is provided, allowing developers to independently define attributes for each page, such as the top bar color or whether pull-to-refresh is enabled.

For more details on other configuration options, refer to the documentation Page Configuration.

It’s important to note that page.json must be used together with WXML, WXSS, and JS files to create a fully functional page.

JSON Syntax

Here are some guidelines for JSON configuration in Mini Program.

JSON files are enclosed within curly braces {} and use key-value pairs to represent data. JSON keys must be wrapped in double quotes. Common mistakes include forgetting to add double quotes around keys or using single quotes instead.

JSON values can only be in the following formats; any other format will result in an error, such as undefined in JavaScript.

  1. 1.Numbers, including floating-point and integer values
  2. 2.Strings, which must be enclosed in double quotes
  3. 3.Boolean values, either true or false
  4. 4.Arrays, enclosed in square brackets []
  5. 5.Objects, enclosed in curly braces {}
  6. 6.Null

It’s also important to note that comments are not allowed in JSON files, as attempting to add them will cause an error.

WXML Templates

Those familiar with web programming know that it relies on a combination of HTML + CSS + JS. Here, HTML is used to describe the structure of a page, CSS defines its appearance, and JS handles interactions between the page and users.

Similarly, Mini Program uses similar components, where WXML serves a role akin to HTML. By opening pages/index/index.wxml, you can see a page template, with a simplified version as follows:

<scroll-view class="scrollarea" scroll-y type="list">
 <view class="container">
 <view class="userinfo">
 <!--... -->
 </view>
 <view class="usermotto">
 <text class="user-motto">{{motto}}</text>
 </view>
 </view>
</scroll-view>

Very similar to HTML, WXML consists of tags and attributes. However, there are also differences, which are explained below:

  1. 1.Different tag names

In HTML, commonly used tags include div, p, and span. These basic tags can be combined to create various components, such as calendars or pop-ups. Considering that everyone needs these components, why not encapsulate them to improve development efficiency?

As shown in the example above, Mini Program ’s WXML uses tags like view, button, and text. These are fundamental building blocks provided by Mini Program for developers, along with additional components such as maps, videos, and audio.

For more components, refer to Components in Mini Program.

  1. 2.Additional properties like wx:if and expressions such as {{}}

In typical web development, JS is used to manipulate the DOM (the tree structure generated from HTML) to respond to user actions, such as changing the interface when a button is clicked. JS records states in variables and uses DOM APIs to modify DOM properties or behaviors, leading to interface changes. As projects grow larger, code becomes cluttered with numerous interaction logic and state variables, which is not an efficient approach. This led to the emergence of MVVM frameworks (such as React and Vue), which separate rendering from logic. In simple terms, JS should no longer directly manipulate the DOM; it only needs to manage states, with a template syntax then used to describe the relationship between states and the interface.

The Mini Program framework follows this principle. For example, to display a “Hello World” string on the interface, WXML is written as:

<text>{{msg}}</text>

JS only needs to manage the state:

this.setData({msg: "Hello World"})

Binding a variable to the interface using <span v-pre>{{}}</span> is called data binding. Data binding alone is not sufficient to fully describe the relationship between states and the interface; control structures such as if/else and for loops are also needed. In Mini Program, these control capabilities are expressed through properties starting with wx:.

For more detailed information, refer to the documentation WXML.

WXSS Styles

WXSS retains most of the features of CSS, but Mini Program has made some additions and modifications to it.

  1. 1.New unit of measurement. When writing CSS styles, it’s necessary to account for varying screen widths and device pixel ratios on mobile devices, requiring techniques to convert pixel units. WXSS supports a new unit, rpx, at the underlying level, allowing developers to avoid conversion hassles and let Mini Program handle the conversion. Due to floating-point calculations, there may be slight discrepancies between the calculated value and the expected value.
  1. 2.Support for global and local styles. Similar to app.json and page.json, you can create an app.wxss file for global styles that apply to all pages in the current Mini Program, while page.wxss files define styles specific to a single page.
  1. 3.WXSS only supports a subset of CSS selectors.

For more detailed information, refer to the documentation WXSS.

JS Logic and Interactions

A service merely having an interface for display is insufficient; it also needs to interact with users—responding to their clicks, obtaining their location, and so on. In Mini Program, user actions are handled by writing JS script files.

<view>{{msg}}</view>
<button bind:tap="clickMe">Click me</button>

When the button is clicked, the msg displayed on the interface should be changed to "Hello World". To achieve this, a property named bind:tap is declared on the button, and a clickMe method is defined in the JS file to handle this click event:

Page({
 clickMe: function() {
 this.setData({msg: "Hello World"})
}
})

Responding to user actions is that simple. For more detailed events, refer to the documentation WXML - Events.

In JS, a Page constructor must be included. For more detailed information on the Page constructor, see the documentation Registering a Page.

Additionally, you can call the rich set of APIs provided by Mini Program in JS. These APIs enable easy access to WeChat’s capabilities, such as retrieving user information, using local storage, and processing WeChat payments. In the earlier QuickStart example, wx.getUserProfile was called in pages/index/index.js to obtain the user’s profile picture and nickname, which were then displayed on the interface via setData. For more APIs, refer to the documentation APIs of Mini Program.

Program

There is a special JS file named app.js:

App()

Throughout Mini Program, there is only one App instance, which is shared across all pages. For more information on event callbacks, refer to the documentation Registering a Program App.

Through this chapter, you have learned about the file types involved in Mini Program and their respective roles. In the next chapter, we will cover topics related to the collaborative work and deployment of Mini Program.