某些情况下,开发者需要将小程序划分成不同的子包,在构建时打包成不同的分包,用户在使用时按需进行加载。
在构建小程序分包项目时,构建会输出一个或多个功能的分包,其中每个分包小程序必定含有一个主包,所谓的主包,即放置默认启动页面/TabBar 页面,以及一些所有分包都需用到公共资源/JS 脚本,而分包则是根据开发者的配置进行划分。
在小程序启动时,默认会下载主包并启动主包内页面,如果用户需要打开分包内某个页面,客户端会把对应分包下载下来,下载完成后再进行展示。
目前小程序分包大小有以下限制:
-
整个小程序所有分包大小不超过 8M
-
单个分包/主包大小不能超过 2M
对小程序进行分包,可以优化小程序首次启动的下载时间,以及在多团队共同开发时可以更好的解耦协作。
打包原则
-
声明 subPackages 后,将按 subPackages 配置路径进行打包,subPackages 配置路径外的目录将被打包到 app(主包) 中
-
app(主包)也可以有自己的 pages(即最外层的 pages 字段)
-
subPackage 的根目录不能是另外一个 subPackage 内的子目录
-
首页的 TAB 页面必须在 app(主包)内
引用原则
-
packageA 无法 require packageB JS 文件,但可以 require app、自己 package 内的 JS 文件
-
packageA 无法 import packageB 的 template,但可以 require app、自己 package 内的 template
-
packageA 无法使用 packageB 的资源,但可以使用 app、自己 package 内的资源
tabbar页面单独放一个包,结构如下
图片中:pages 为主包,部分bufen_pack为副包
app.json 代码:
-
{
-
"pages": [
-
"pages/index/index",
-
"pages/fenlei/fenlei",
-
"pages/index/xcxdl",
-
"pages/index/shoplist",
-
"pages/index/post",
-
"pages/index/type_list",
-
"pages/category_page4/category_page4",
-
"pages/index/sign_in",
-
"pages/recommend/recommend",
-
"pages/trends/trends",
-
"pages/trends/comment",
-
"pages/trends/thumb_up",
-
"pages/trends/personal_letter",
-
"pages/trends/replylist",
-
"pages/my/set_authorize",
-
"pages/my/manager_list",
-
"pages/my/my",
-
"pages/my/my_index",
-
"pages/my/m_publish",
-
"pages/my/psl_letter",
-
"pages/my/follow_fans_list",
-
"pages/my/sc",
-
"pages/my/psl_detail",
-
"pages/my/bind_phone",
-
"pages/my/edit_phone",
-
"pages/index/search",
-
"pages/my/edit_nick_name",
-
"pages/auth/auth",
-
"pages/yhsh/yhsh",
-
"pages/confirm_order/confirm_order",
-
"pages/shopping/index",
-
"pages/product_detail/product_detail",
-
"pages/goodlist/goodlist",
-
"pages/shopmall/index"
-
],
-
"window": {
-
"backgroundTextStyle": "light",
-
"navigationBarBackgroundColor": "#5bade4",
-
"navigationBarTitleText": "首页",
-
"navigationBarTextStyle": "#fff"
-
},
-
"tabBar": {
-
"color": "#8a8a8a",
-
"selectedColor": "#EE6E5E",
-
"list": [
-
{
-
"pagePath": "pages/index/index",
-
"text": "首页",
-
"iconPath": "icons/default1.png",
-
"selectedIconPath": "icons/select1.png"
-
},
-
{
-
"pagePath": "pages/fenlei/fenlei",
-
"text": "分类",
-
"iconPath": "icons/default2.png",
-
"selectedIconPath": "icons/select2.png"
-
},
-
{
-
"pagePath": "pages/index/type_list",
-
"text": "发布信息",
-
"iconPath": "icons/icon5.png",
-
"selectedIconPath": "icons/icon5.png"
-
},
-
{
-
"pagePath": "pages/category_page4/category_page4",
-
"text": "商城",
-
"iconPath": "icons/default3.png",
-
"selectedIconPath": "icons/select3.png"
-
},
-
{
-
"pagePath": "pages/my/my",
-
"text": "我的",
-
"iconPath": "icons/default4.png",
-
"selectedIconPath": "icons/select4.png"
-
}
-
]
-
},
-
"subPackages": [
-
{
-
"root": "bufen_pack", //加载副包的路径
-
"pages": [
-
"address_edit/address_edit",
-
"address/address",
-
"cart/cart",
-
"home/home",
-
"nearby/nearby",
-
"nearby/board_list",
-
"nearby/rz",
-
"nearby/s_detail",
-
"nearby/s_d_detail",
-
"nearby/contact"
-
]
-
}
-
]
-
}
阅读(3241) | 评论(0) | 转发(0) |