# Independent subcontract
WeChat Client 6.7.2, Base Library 2.3.0 The above version is supported. Developer Tools Please use 1.02.1808300 And above version, may Click here to download。
Stand-alone sub-packageing is a special type of sub-packageing in Mini Programs that can run independently of the main package and other sub-packageing. There is no need to download the main package when entering an Mini Program from a separate subcontract page. The master package is downloaded only when the user enters the general subcontract or master package page.
Developers can configure certain functionally independent pages into independent sub-packages on demand. When the Mini Program is launched from a normal sub-packageing page, it needs to download the main package first.The independent subcontract can run without relying on the main package, which can greatly improve the start-up speed of the subcontract page.
There can be multiple independent sub-packages in a Mini Program.
Small games in the base library v2.12.2 Begin to support independent sub-packageing. See [Small Mini game Independent Subcontracting Guide](https://developers.weixin.qq.com/Minigame /dev/Guide/base-ability/independent-sub-packages.html)。
# Configuration method
Suppose the Mini Program directory structure is as follows:
app.js
app.json
app.wxss
moduleA
pages
rabbit
squirrel
moduleB
pages
pear
pineapple
pages
index
logs
utils
Developers through theapp.json
ofsubpackages
Defined in the subcontract configuration entry corresponding to theindependent
Field declares the corresponding subcontract as an independent subcontract.
{
"pages": [
"pages/index",
"pages/logs"
],
"subpackages": [
{
"root": "moduleA"
"pages": [
"pages/rabbit",
"pages/squirrel"
]
}, {
"root": "moduleB"
"pages": [
"pages/pear",
"pages/pineapple"
],
"independent": true
}
]
}
# limit
Independent sub-packageing is a type of sub-packageing. All limitations of an ordinary subcontract are valid for an independent subcontract. Plug-ins and custom components in independent sub-packageing are handled in the same way as ordinary sub-packageing.
In addition, when using independent sub-packageing, be aware that:
- Independent sub-packages cannot rely on the contents of the master and other sub-packages, including js Files, templates, wxss, custom components, plugins, etc. (using Subcontract asynchrony time js Files, custom components, and plug-ins are not subject to this clause.
- In the main package
app.wxss
Is not valid for independent sub-packages and should be avoided in the Independent Subcontracts pageapp.wxss
The style in App
Can only be defined within the master package, not in the independent subcontractApp
, can cause unexpected behavior- The use of plug-ins is not supported in standalone sub-packages.
# Note
# (1) Regarding getApp()
Unlike ordinary sub-packageing, when an independent sub-packageing is run,App
Are not necessarily registered. Therefore, getApp()
It is not necessarily possible to obtain App
Object:
- When the user launches the Mini Program from the independent subcontract page, the main package does not exist,
App
Does not exist, at this point the callgetApp()
What you get isundefined
。 The main package is downloaded when the user enters the general subcontract or main package page.App
To be registered. - When the user is jumping from a normal subpackage or a main package page to a separate subpackage page, the main package already exists.
getApp()
You can get real.App
。
Because of this limitation, developers are unable to pass App
Object enables independent sub-packageing and global variable sharing with the rest of the Mini Program.
To meet this need in independent sub-packageing, the base library 2.2.4 Version begins. getApp
Support [allowDefault
] Parameters, in the App
Returns a default implementation when undefined. When the primary package is loaded,App
When registered, the properties defined in the default implementation are overridden and merged into the real App
In.
Sample code:
- Independent subcontracting
const app = getApp({allowDefault: true}) // {}
app.data = 456
app.global = {}
- app.js in
App({
data: 123,
other: 'hello'
})
console.log(getApp()) // {global: {}, data: 456, other: 'hello'}
# (2) Regarding App
life cycle
When starting an Mini Program from an independent subcontractor, the main package App
of onLaunch
And for the first time onShow
Called when you first enter the main package or other normal subcontract pages from a separate subcontract page.
As it is not possible to define in the independent subcontract App
, the monitoring of the Mini Program life cycle can be used wx.onAppShow,wx.onAppHide Done.App
Other events on the wx.onError,wx.onPageNotFound Listening.
# Low Version Compatible
In less than 6.7.2 When running in the version of WeChat, independent sub-packageing is regarded as an ordinary sub-packageing process and does not have the characteristics of independent operation.
Note: In compatibility mode, the main package of the app.wxss
May have an impact on pages in a separate subcontract, and should therefore be avoided in a separate subcontract app.wxss
The style in.