HMR API的webpack文档提到了以下方法:
accept(dependencies: string[], callback: (updatedDependencies) => void) => void
我理解如何接受单个依赖项但不确定多个依赖项的回调应该如何。
这是我的代码:
var $ = require('jquery')
var page = require('page')
var index = require('./index')
var home = require('./home')
$(function() {
page('/', index)
page('/home', home)
page()
if (module.hot) {
module.hot.accept(['./index', './home'], function(updatedDependencies) {
// what should I put in here?
})
}
})
答案 0 :(得分:4)
正在回答这个问题,因为没有其他人尝试过。
从查看代码看起来很好。
在接受多个依赖项之后运行回调方法。 您基本上将一个函数作为参数发送,然后一旦接受了所有依赖项,就会执行该函数。
所以你几乎可以在这个功能中放任何你喜欢的东西。例如:
if (module.hot) {
module.hot.accept(['./index', './home'], function() {
alert('all the dependencies have been accepted');
console.log('all the dependencies have been accepted');
});
};
在该示例中,一旦accept方法运行并且已经完成,它将执行回调函数,在这种情况下,该函数发送警报并将消息记录到控制台。
简而言之,请替换'//我应该在这里放什么?'一旦接受了依赖项,就要继续使用代码。
我在你的回调中看到你有一个参数'updatedDependencies'你可以调试并在你的回调方法的第一行放置一个断点,然后将鼠标放在'updatedDependencies'参数上以查看它是否包含任何内容 - 如果它是的,显然你可以相应地使用这些数据。
希望这有帮助。