A list,

1. Directory structure

First, we used the developer tools provided by wechat public platform to create a simple small program project and observe the directory structure of the project

It is not hard to see that a typical wechat applet usually contains a main section describing the whole and a Pages folder describing the page

  • Body: A program used to describe the whole, located in the root directory, usually consisting of three files

    • app.js: Logical code files for small programs that comply with JavaScript language specifications
    • app.json: common configuration file of a small program, which complies with the JSON language specification
    • app.wxss: common style file for small programs, compatible with CSS syntax specifications
  • Pages: Used to describe each page in the applet, located in the Pages directory

    A page corresponds to a folder in the Pages directory, and a folder usually consists of four files

    • pageName.js: the logical code file of the page, responsible for processing the logical content of the page
    • pageName.json: configuration file of the page, used to set the content of the page window
    • pageName.wxml: the structure file of the page that describes the organization of the visual components of the page
    • pageName.wxss: a style file for a page that describes the presentation of the visual components of the page
  • Other:

    • project.config.json: Save the configuration of wechat developer tools
    • Util folder: Stores some generic utility functions

The core of the small program is mainly four different formats of files, respectively: JS, JSON, WXML, WXSS

2. Development framework

MINA framework is used as the core framework for wechat mini programs. The whole framework system is divided into three parts:

  • View layer (View) : Usually includes WXML and WXSS files for rendering page presentation
  • Logic layer (App Service) : Usually contains JS files for processing business logic
  • System layer (Native) : The Native capability of wechat client is added. The view layer and logic layer communicate with each other through JSBridge of system layer

At the heart of the framework is a responsive data binding system implemented through JSBridge that keeps data in sync between the logical layer and the view layer

Second, the configuration

Json is the extension name of the configuration file of the small program, and the file format complies with the JSON syntax specification

Configuration files can be divided into applet global configuration file app.json and page configuration file pageName

1. Global configuration files

The global configuration file app.json is used to configure applets globally

// app.json
{
  "pages": ["pages/index/index"."pages/logs/logs"]."window": {"backgroundTextStyle":"light"."navigationBarBackgroundColor": "#fff"."navigationBarTitleText": "WeChat"."navigationBarTextStyle":"black"}}Copy the code

App. json has the following common configuration items:

attribute type mandatory describe
pages String Array is Configuration page
window Object no Window presentation configuration
tarbar Object no Navigation Bar Configuration
networkTimeout Object no Network Timeout Setting
debug Boolean no Debug Mode Configuration

(1) pages

Pages is a page configuration that specifies which pages the applet consists of

To add or delete pages in a small program, you need to modify the contents of the Pages array. Otherwise, the pages array cannot be loaded normally, and errors may be reported

(2) the window

Window is a window presentation configuration, and its commonly used properties are as follows:

attribute type The default value describe
navigationBarBackgroundColor HexColor # 000000 Defines the background color of the navigation bar
navigationBarTextStyle String white Defines the navigation bar title text color
navigationBarTitleText String Define the navigation bar title text content
backgroundColor HexColor #ffffff Defines the background color of the window
backgroundTextStyle String dark Define dropdown background fonts and loading image styles
enablePullDownRefresh Boolean false Defines whether to enable drop-down refresh

(3) the tarBar

TarBar is a navigation bar configuration. The tarBar object is an array that can be configured with at least 2 and at most 5 tabs. Its commonly used properties are as follows:

attribute type The default value describe
color HexColor Set the color of the navigation text
selectedColor HexColor Sets the color of navigation text when selected
backgroundColor HexColor Set the background color of the navigation bar
borderStyle String black Sets the top border color of the navigation bar
position String bottom Set the location of the navigation bar on the page
list Object Array Set navigation bar Navigation buttons

The common attributes of the objects in the list array are as follows:

attribute type describe
pagePath String Specify page path
text String Specifying button text
iconPath String Specify button icon
selectedIconPath String Specifies the button icon when selected

(4) networkTimeout

NetworkTimeout is a networkTimeout setting. Its common properties are as follows:

attribute type The default value describe
request Number 60000 Set the wx.request timeout in milliseconds
connectSocket Number 60000 Set the timeout period of wx.connectSocket, in milliseconds
uploadFile Number 60000 Set the timeout period for wx.uploadFile, in milliseconds
downloadFile Number 60000 Set the timeout for wx.downloadFile in milliseconds

(5) the debug

Debug is used to enable the debug mode of the applets development tool

2. Page configuration file

The page configuration file pageName. Json is used to configure for a particular page by modifying the contents of the Window option

In addition, because only window-related configuration items can be set in the page configuration file, the window key is not required

Its common attributes are as follows:

attribute type The default value describe
navigationBarBackgroundColor HexColor # 000000 Defines the background color of the navigation bar
navigationBarTextStyle String white Defines the navigation bar title text color
navigationBarTitleText String Define the navigation bar title text content
backgroundColor HexColor #ffffff Defines the background color of the window
backgroundTextStyle String dark Define dropdown background fonts and loading image styles
enablePullDownRefresh Boolean false Defines whether to enable drop-down refresh
disableScroll Boolean false Defines whether a page is allowed to scroll up and down as a whole

Third, the visual layer

There are two types of visual layer files, which are.wxml files and.wxss files

. WXML file is responsible for the description of the page structure, based on XML language specification; The. WXSS file is responsible for the description of visual styles, compatible with THE CSS language specification

1, WXML

WXML (WeiXin Markup Language) is a tag Language that combines component and event systems to build the structure of a page

(1) Visual components

As mentioned earlier, WXML is based on the XML language specification, so WXML is very similar to XML in terms of syntax rules:

< tag name attribute =" attribute Value "attribute =" attribute value ">...... </ Tag name >Copy the code

See this syntax, I believe that contact HTML friends will not be unfamiliar, can be said to have the same wonderful ah

The label is the visual component in wechat applet, which is the basic component unit of the view layer of the applet. Properties control the presentation of components

We will not expand on the function and usage of each tag (visual component) here, because it is clearly written in the official wechat document

However, let’s talk about some of the features of WXML

(2) WXML features

① Data Binding

Data binding is an important feature of WXML, passing operations from the view layer to the logical layer and rendering data from the logical layer to the view layer

We do this by using Mustache syntax (double braces) in a WXML file

[Example code]

<! --index.wxml-->
<view hidden="{{flag}}">
    Hidden
</view>
Copy the code
// index.js
Page({
    data: {flag: "false"}})Copy the code

[Display effect]

Hidden
Copy the code
② List rendering

List rendering is implemented through the WX :for property, which binds to an array and repeatedly renders the component using the items in the array

The wx:for-item attribute specifies the current element of the array, and the wx:for-index attribute specifies the current index of the array

If the position of the items in the list changes dynamically and we want the items in the list to maintain their identity and status,

You need to specify the unique identifier of the item in the list using the wx:key attribute. The value of the Wx :key attribute is provided in two forms:

  • String: A property representing an item whose value must be a unique string or number in the list and cannot be changed dynamically
  • Keyword this: represents item itself, which requires item itself to be a unique string or number

[Example code]

<! --index.wxml-->
<view>
    <block wx:for="{{items}}" wx:for-item="item" wx:for-index="index">
        <view>{{index}}:{{item.name}}</view>
    </block>
</view>
Copy the code
// index.js
Page({
    data: {items:[
            {name: "A"},
            {name: "B"},
            {name: "C"},
            {name: "D"},
            {name: "E"}]}})Copy the code

[Display effect]

0:A
1:B
2:C
3:D
4:E
Copy the code
③ Conditional rendering

Conditional rendering is implemented with the Wx :if attribute. When the value of the Wx :if binding is true, the corresponding Wx :else attribute can be added to render components

If you want to use a single judgment statement to determine whether several component tags need to be rendered, you can wrap multiple components with <block /> tags

Wx: Difference between the if property and the hidden property:

  • Wx :if starts rendering and displaying components when the binding value is true
  • Hidden Prerenders the component regardless of whether the binding value is true or false, if true, hides the component, if false, shows the component

[Example code]

<! --index.wxml-->
<view wx:if="{{condition == 1}}">A</view>
<view wx:elif="{{condition == 2}}">B</view>
<view wx:else>C</view>
Copy the code
// index.js
Page({
    data: {condition:0}})Copy the code

[Display effect]

C
Copy the code
④ Template Reference

WXML uses templates to define code snippets. Templates can be used to reduce code duplication, with the following attributes:

  • Name: Declares the name of the template
  • Is: declares the template to be used
  • Data: Declares the data to be passed in

[Example code]

<! --index.wxml-->
<template name="tempItem">
    <view>
		<view>Name: {{name}}</view>
		<view>Telephone: {{phone}}</view>
		<view>Address: {{address}}</view>
    </view>
</template>

<template is="tempItem" data="{{... item}}"></template>
Copy the code
// index.js
Page({
    item: {name: "Zhang".phone: "12345678910".address: "China"}})Copy the code

[Display effect]

Name: Zhang SAN phone: 12345678910 Address: ChinaCopy the code

The purpose of using a template is to reference a template in another file. You can reference a template in two ways: import and include

The import mode can reference the template defined in the object file in this file, but not the template referenced in the object file

Include includes all the code in the target file except <template>, which is equivalent to copying to the current file

2, WXSS

WXSS (WeiXin Style Sheet) is a Style language used to describe the Style of WXML components

(1) Basic introduction

As mentioned earlier, WXSS is compatible with the CSS language specification, so WXSS is very similar to CSS in terms of syntax rules:

Selector { property: value; property: value; . }Copy the code

WXSS and CSS are basically the same

An element is selected by Selector, and the style of the corresponding element is specified by a property:value key-value pair

(2) selector

Currently, the selectors supported by wechat applet include. Class, #id, Element,.element, ::after, ::before

In addition, wechat small program also support! Important statement, just appear! The style of the important declaration, indicating that it has the highest priority

(3) Common attributes

① Basic attributes

Base properties are properties that all components have

The property name type describe
id String The unique identity of the component
class String The component’s style sheet
style String Inline styles for components
hidden Boolean Whether the component displays
data – * Any Custom attributes
hide * / catch * EventHandler Component events

Note: Static styles are usually written to the class property and dynamic styles to the style property

② Dimension attribute
attribute describe
height The element height
width The width of the element
line-height Line height
max-height Maximum height
max-width Maximum width
min-height The minimum height
min-width Minimum width
③ Background attributes
attribute describe
background-color The background color
background-image The background image
④ Text format
attribute describe
color Text color
text-align Text alignment
text-indent Text indentation
⑤ Font attributes
attribute describe
font-family HTTP:
font-size The font size
⑥ Layout attributes
attribute describe
display Display mode
position Location type
float Image floating position
border-style Border style
border-width Border width
border-color Border color
margin padding
padding From the outside

After setting the position property, you can use the following properties to locate:

attribute describe
bottom The offset between the lower margin of an element and the lower margin of its containing block
top The offset between the upper margin of an element and the upper margin of its containing block
left The offset between the left margin of an element and the left margin of its containing block
right The offset between the right margin of an element and the right margin of its containing block
clip Clip an absolutely positioned element
overflow What happens when element content overflows its area

Fourth, the logical layer

The logical layer of the applets development framework consists of.js files in the root directory and the page folder. The file format complies with the JavaScript language specification

The logical layer is responsible for the data processing of the applet. It receives data from the view layer and returns the processed results to the view layer

1, registered

Applets register programs and pages using the App() and Page() methods, and get an instance of the App using the global method getApp()

(1) small program registration

Applets are registered through the App() function and must be registered in the app.js file in the root directory, and can only be registered once

It takes an object parameter, which specifies the life cycle function of the applet. Its common properties are as follows:

attribute type describe trigger
onLaunch Function Life cycle function – Listens for applets initialization When the applet initialization is complete
onShow Function Life cycle function – Listens for applet display When the applets start or enter the foreground from the background
onHide Function Life cycle function – Listens for applets to hide When applets go from the foreground to the background

(2) Page registration

Before a Page can be used in a program, it must be registered with the Page() function

It takes an object parameter, which specifies the page’s initial data, lifecycle functions, and event handlers. Common properties are as follows:

attribute type describe
data Object Initial data for the page
onLoad Function Life cycle function – listens for page loads
onReady Function Life cycle function – Listens for the page to complete its first rendering
onShow Function Life cycle function – Listens for page display
onHide Function Life cycle function – Listens for page hiding
onUnload Function Life cycle function – Listens for page unload
onPullDownRefresh Function Event handler – Listens for user drop-down refresh
onReachBottom Function Event handler – Listens for the user to pull up to the bottom

2. Page routing

Applets maintain the page in the form of a stack, following the principle of last in, first out. When the page is switched, the page stack behaves as follows:

Page routing Page stack representation
Initialize the New page is pushed
Open a new page New page is pushed
Page redirection The current page is out of the stack, and the new page is in the stack
The page returns The page is pushed out of the stack until the target page is returned and a new page is pushed in
The Tab to switch The current page is out of the stack, and the new page is in the stack

Applets are routed as follows:

routing trigger Pre-route page Post route page
Initialize the The applet opens the first page OnLoad, onShow
Open a new page Call THE API Wx. navigateTo or use the component onHide OnLoad, onShow
Page redirection Call API Wx. redirectTo or use a component onUnload OnLoad, onShow
The page returns Call API Wx. navigateBack or use the component or the user presses the upper-left return button onUnload onShow
The Tab to switch Call API wx.switchTab or use component or user to switchTab OnLoad, onShow for the first time; Otherwise onShow
Page reload Call API Wx. reLaunch or use a component onUnload OnLoad, onShow

3, events,

The event system is the communication mode between the view layer and the logical layer

It can feed back the user’s actions from the view layer to the logic layer and reflect the processing results of the logic layer to the view layer

Events can be bound to components, and when an event is fired, the corresponding event handler in the logical layer is executed

Event handlers usually contain an object with the following attributes:

attribute type describe
type String The event type
timeStamp Integer The time stamp
target Object The source component
currentTarget Object The current component
touches Object Array The touch event is unique to the information about the touch point currently on the screen
changedTouches Object Array The touch event is unique to the current changing touch point information
detail Object Custom event unique, additional information

Native components and native apis

The content of native components and native API is very clear and easy to understand in the official documents of wechat public platform, so I will not introduce more here

Here’s a direct link:

The primary components: developers.weixin.qq.com/miniprogram…

Native API:developers.weixin.qq.com/miniprogram…