首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

热模块替换 | Hot Module Replacement

如果已经通过 HotModuleReplacementPlugin 启用了模块热替换(Hot Module Replacement),则它的接口将被暴露在 module.hot 属性下面。通常,用户先要检查这个接口是否可访问,然后再开始使用它。举个例子,你可以这样 accept 一个更新的模块:

代码语言:javascript
复制
if (module.hot) {
  module.hot.accept('./library.js', function() {
    // Do something with the updated library module...
  })
}

支持以下方法...

accept

接受(accept)给定依赖模块的更新,并触发一个 回调函数 来对这些更新做出响应。

代码语言:javascript
复制
module.hot.accept(
  dependencies, // Either a string or an array of strings
  callback // Function to fire when the dependencies are updated
)

decline

拒绝给定依赖模块的更新,使用 'decline' 方法强制更新失败。

代码语言:javascript
复制
module.hot.decline(
  dependencies // Either a string or an array of strings
)

dispose(或addDisposeHandler

添加一个处理函数,在当前模块代码被替换时执行。此函数应该用于移除你声明或创建的任何持久资源。如果要将状态传入到更新过的模块,请添加给定 data 参数。更新后,此对象在更新之后可通过 module.hot.data 调用。

代码语言:javascript
复制
module.hot.dispose(data => {
  // Clean up and pass data to the updated module...
})

removeDisposeHandler

删除由 disposeaddDisposeHandler 添加的回调函数。

代码语言:javascript
复制
module.hot.removeDisposeHandler(callback)

status

取得模块热替换进程的当前状态。

代码语言:javascript
复制
module.hot.status()//将返回以下字符串之一...

状态

描述

该过程正在等待呼叫检查(见下文)

该过程正在检查更新

准备

该过程正在准备进行更新(例如,下载更新的模块)

准备

更新已准备好并可用

该过程正在调用将被替换的模块上的处理处理程序

应用

该过程正在调用接受处理程序并重新执行自我接受的模块

流产

更新被中止,但系统仍处于之前的状态

失败

更新引发异常并且系统的状态已被破坏

check

测试所有加载的模块以进行更新,如果存在更新,则apply它们。

代码语言:javascript
复制
module.hot.check(autoApply).then(outdatedModules => {
  // outdated modules...
}).catch(error => {
  // catch errors
});

autoApply 参数可以是布尔值,也可以是 options,当被调用时可以传递给 apply 方法。

apply

继续更新过程(只要module.hot.status() === 'ready')。

代码语言:javascript
复制
module.hot.apply(options).then(outdatedModules => {
  // outdated modules...
}).catch(error => {
  // catch errors
});

可选options对象可以包含以下属性:

  • ignoreUnaccepted (布尔型):忽略对未接受模块所做的更改。
  • ignoreDeclined (布尔):忽略对已拒绝模块的更改。
  • ignoreErrored (布尔值):忽略错误引发接受处理程序,错误处理程序和reevaulating模块。
  • onDeclined (函数(info)):拒绝模块的通知程序
  • onUnaccepted (function(info)):未接受模块的通告程序
  • onAccepted (函数(信息)):用于接受模块的通知程序
  • onDisposed (函数(信息)):处置模块的通告程序
  • onErrored (函数(信息)):通知程序的错误

info参数将是包含以下某些值的对象:

代码语言:javascript
复制
{
  type: "self-declined" | "declined" | 
        "unaccepted" | "accepted" | 
        "disposed" | "accept-errored" | 
        "self-accept-errored" | "self-accept-error-handler-errored",
  moduleId: 4, // The module in question.
  dependencyId: 3, // For errors: the module id owning the accept handler.
  chain: [1, 2, 3, 4], // For declined/accepted/unaccepted: the chain from where the update was propagated.
  parentId: 5, // For declined: the module id of the declining parent
  outdatedModules: [1, 2, 3, 4], // For accepted: the modules that are outdated and will be disposed
  outdatedDependencies: { // For accepted: The location of accept handlers that will handle the update
    5: [4]
  },
  error: new Error(...), // For errors: the thrown error
  originalError: new Error(...) // For self-accept-error-handler-errored: 
                                // the error thrown by the module before the error handler tried to handle it.
}

addStatusHandler

注册一个函数来侦听更改status

代码语言:javascript
复制
module.hot.addStatusHandler(status => {
  // React to the current status...
})

removeStatusHandler

移除一个注册的状态处理函数。

代码语言:javascript
复制
module.hot.removeStatusHandler(callback)

扫码关注腾讯云开发者

领取腾讯云代金券