Componentization
The interface code organization of Mini Program uses Components as the basic unit.
A page can be composed of multiple components. These components are divided into two categories:
- Basic components are atomic functional units that cannot be further decomposed, such as
<view>and<image>; - Multiple components can be assembled to form a larger component, which is referred to as a Custom component.
In other words, a page is composed of basic components and custom components, while custom components are themselves composed of basic components and other custom components. Specifically:
- A page is essentially a large custom component, also known as the Page Root Component. Therefore, a page possesses all the characteristics of custom components;
- A custom component can recursively reference itself (however, infinite recursion will lead to a stack overflow, so ensure you control the recursion conditions properly).
Features related to custom components have been supported since base library version 1.6.3 of Mini Program.
Component Code Structure
The code for each custom component consists of four parts:
- JSON configuration;
- WXML template
- WXSS styles
- JavaScript (or TypeScript) script
These four files must be placed in the same file path and differ only by their file extensions.
Among them, the JSON configuration must include at least one of usingComponents or component.
For the page root component, it is typically written as follows:
{
"usingComponents": {}
}
For custom components that are not the page root, it is typically written as follows:
{
"component": true,
"usingComponents": {}
}
usingComponents specifies the other components that this component depends on. Refer to the Component Reference documentation for details. From another perspective, once the JSON configuration, WXML template, WXSS styles, and JavaScript script of a component are completed, it can be referenced by other components.
The component field is not mandatory. It only affects certain framework details; even if it is not specified, as long as the usingComponents field is present, it can still be used as a component. However, it is still recommended to add the component field to all non-page-root components as an identifier.
Component Framework Selection
Currently, two component frameworks are available.
exparser is the traditional component framework, offering the best compatibility with legacy code. However, due to historical reasons, it lacks some newer features and does not deliver optimal performance.
glass-easel is the next-generation component framework, featuring more capabilities and better performance. To learn more about the framework itself, refer to the glass-easel open-source project.
When using the traditional WebView rendering engine, you can choose to use either component framework for rendering each page. By default, exparser is used as the component framework. To switch to glass-easel, you need to declare it in the page JSON configuration:
{
"usingComponents": {},
"componentFramework": "glass-easel",
"glassEaselWebview": true
}
Additionally, ensure that on-demand injection (1003681) is enabled for your project by adding the following to app.json:
{
"lazyCodeLoading": "requiredComponents"
}
For detailed explanations of the above configurations, refer to the Migrating to glass-easel documentation.
If you are using the Skyline rendering engine, glass-easel is the only available component framework.
The componentFramework field can be placed in app.json to apply globally.
Custom components within a page follow the component framework selected by that page.
Although these two component frameworks are highly similar, there are some subtle differences between them. Refer to Migrating to glass-easel to upgrade your component framework.