4

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?
     })
   }
 })
4

1 回答 1

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');
    });
};

在该示例中,一旦接受方法运行并完成,它将执行回调函数,在这种情况下,回调函数会发送警报并将消息记录到控制台。

所以简而言之,替换'//我应该在这里放什么?接受依赖项后,使用您要继续使用的代码。

我在您的回调中看到您有一个参数“updatedDependencies”,您可以调试并在回调方法的第一行放置一个断点,然后将鼠标放在“updatedDependencies”参数上以查看它是否包含任何内容 - 如果包含,您显然可以相应地使用该数据。

希望这可以帮助。

于 2016-02-17T15:34:57.063 回答