项目结构、生命周期和捆绑

迷你应用项目结构

与之前涉及的标记语言、样式语言和组件一样;就迷你应用项目结构而言,文件扩展名或默认名称等详细信息也会有所不同。不过,总体思路对于所有超级应用提供商都是相同的。项目结构始终由以下部分组成:

  • 用于初始化迷你版应用的根文件 app.js
  • app.json大致对应网络应用清单的配置文件。
  • 具有共享默认样式(可选)的通用样式表文件 app.css
  • 一个包含 build 信息的 project.config.json 文件。

所有页面都存储在 pages 文件夹中的各个子文件夹中。每个页面子文件夹都包含一个 CSS 文件、一个 JS 文件、一个 HTML 文件和一个可选的配置 JSON 文件。除了文件扩展名之外,所有文件的名称都必须与其所在文件夹相同。因此,迷你应用只需要一个指向 app.json 文件(类清单文件)中的目录的指针,并且可以动态查找所有子资源。从 Web 开发者的角度来看,迷你应用因此是多页面应用。

├── app.js               # Initialization logic
├── app.json             # Common configuration
├── app.css              # Common style sheet
├── project.config.json  # Project configuration
└── pages                # List of pages
   ├── index               # Home page
   │   ├── index.css         # Page style sheet
   │   ├── index.js          # Page logic
   │   ├── index.json        # Page configuration
   │   └── index.html        # Page markup
   └── other               # Other page
       ├── other.css         # Page style sheet
       ├── other.js          # Page logic
       ├── other.json        # Page configuration
       └── other.html        # Page markup

迷你应用生命周期

必须通过调用全局定义的 App() 方法向超级应用注册迷你应用。请参阅前面概述的项目结构,这发生在 app.js 中。迷你应用生命周期本质上由四个事件组成:launchshowhideerror。这些事件的处理程序可以以配置对象的形式传递给 App() 方法,该对象还可以包含 globalData 属性,用于存储应在所有页面全局可用的数据。

/* app.js */
App({
  onLaunch(options) {
    // Do something when the app is launched initially.
  },
  onShow(options) {
    // Do something when the app is shown.
  },
  onHide() {
    // Do something when the app is hidden.
  },
  onError(msg) {
    console.log(msg);
  },
  globalData: "I am global data",
});

一如既往,各项指标的具体细节会有所不同,但对于WeChat支付宝百度ByteDance快捷应用,其概念都是相同的。

页面生命周期

与应用生命周期类似,页面生命周期也有生命周期事件,开发者可以监听这些事件并做出响应。这些核心事件包括 loadshowreadyhideunload。某些平台会提供其他事件,例如 pulldownrefresh。您可以在为每个页面定义的 Page() 方法中设置事件处理脚本。对于之前项目结构中的 indexother 页面,这会分别发生在 index.jsother.js 中。

/* index.js */
Page({
  data: {
    text: "This is page data.",
  },
  onLoad: function (options) {
    // Do something when the page is initially loaded.
  },
  onShow: function () {
    // Do something when the page is shown.
  },
  onReady: function () {
    // Do something when the page is ready.
  },
  onHide: function () {
    // Do something when the page is hidden.
  },
  onUnload: function () {
    // Do something when the page is closed.
  },
  onPullDownRefresh: function () {
    // Do something when the user pulls down to refresh.
  },
  onReachBottom: function () {
    // Do something when the user scrolls to the bottom.
  },
  onShareAppMessage: function () {
    // Do something when the user shares the page.
  },
  onPageScroll: function () {
    // Do something when the user scrolls the page.
  },
  onResize: function () {
    // Do something when the user resizes the page.
  },
  onTabItemTap(item) {
    // Do something when the user taps the page's tab.
  },
  customData: {
    foo: "bar",
  },
});

构建流程

迷你应用的构建流程脱离了开发者。从本质上讲,它使用各种行业工具,例如用于转译和缩减的 Babel 编译器以及 postcss CSS 转换器。其构建体验与 Next.jscreate-react-app 的体验类似,如果开发者明确选择不更改构建体验,就永远不要处理构建参数。生成的已处理文件最终会进行签名、加密,并打包到一个或多个(子)软件包中,然后上传到超级应用提供商的服务器。子软件包旨在实现延迟加载,因此不必一次性下载所有迷你应用。包装详细信息应保密且不会进行记录,但某些软件包格式(例如微信的 wxapkg 格式)已进行了逆向工程

致谢

本文由 Joe MedleyKayce BasquesMilica MihajlijaAlan Kent 和 Keith Gu 审核。