# PreDownloadSubpackageTask wx.preDownloadSubpackage(Object object)

基础库 2.27.3 开始支持,低版本需做兼容处理

微信 Windows 版:支持

微信 Mac 版:支持

# 功能描述

触发分包预下载。

# 参数

# Object object

属性 类型 默认值 必填 说明
packageType string 'normal' 分包的类型,填写 "workers" 后 name 字段失效
合法值 说明
workers worker 分包
normal 普通分包, 3.4.9及以上版本支持
name string 分包的名字,可以填 name 或者 root。在独立分包内,填 \_\_GAME\_\_ 表示加载主包,详见 小游戏独立分包指南, 3.4.9及以上版本支持
success function 分包加载成功回调事件
fail function 分包加载失败回调事件
complete function 分包加载结束回调事件(加载成功、失败都会执行)

# 返回值

# PreDownloadSubpackageTask

预下载分包任务实例,用于获取分包预下载状态

# 注意事项

  • wx.preDownloadSubpackage 与 wx.loadSubpackage 的区别: wx.preDownloadSubpackage 只下载代码包,不自动执行代码;wx.loadSubpackage 下载完代码包后会自动执行代码。

# worker 分包示例代码

// 首先要在 app.json / game.json 中配置workers作为分包
{
  "workers": {
    "path": "myWorkersFolder",
    "isSubpackage": true  // true 表示把 worker 打包为分包。默认 false。填 false 时等同于 { "workers": "myWorkersFolder" }
  }
}
// 然后调用 wx.preDownloadSubpackage 下载 worker 分包,下载成功后才可以创建 worker
var task = wx.preDownloadSubpackage({
  packageType: "workers",
  success(res) {
    console.log("load worker success", res)
    wx.createWorker("myWorkersFolder/request/index.js")   // 创建 worker。 如果 worker 分包没下载完就调 createWorker 的话将报错
  },
  fail(res) {
    console.log("load worker fail", res)
  }
})

task.onProgressUpdate(res => {
  console.log(res.progress) // 可通过 onProgressUpdate 接口监听下载进度
  console.log(res.totalBytesWritten)
  console.log(res.totalBytesExpectedToWrite)
})
普通分包示例代码 <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 首先要在 app.json / game.json 中配置分包</span> <span class="token punctuation">{</span> <span class="token string">"subPackages"</span><span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">{</span> <span class="token string">"name"</span><span class="token operator">:</span> <span class="token string">"ModuleA"</span><span class="token punctuation">,</span> <span class="token string">"root"</span><span class="token operator">:</span> <span class="token string">"/ModuleA/"</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span> </code></pre></div><div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> task <span class="token operator">=</span> wx<span class="token punctuation">.</span><span class="token function">preDownloadSubpackage</span><span class="token punctuation">(</span><span class="token punctuation">{</span> name<span class="token operator">:</span> <span class="token string">"ModuleA"</span><span class="token punctuation">,</span> <span class="token function">success</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"load subpackage success"</span><span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token function">fail</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"load subpackage fail"</span><span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> task<span class="token punctuation">.</span><span class="token function">onProgressUpdate</span><span class="token punctuation">(</span><span class="token parameter">res</span> <span class="token operator">=></span> <span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>progress<span class="token punctuation">)</span> <span class="token comment">// 可通过 onProgressUpdate 接口监听下载进度</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>totalBytesWritten<span class="token punctuation">)</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>totalBytesExpectedToWrite<span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> </code></pre></div>