2016-02-11 57 views
3

的HMR API中的WebPack文檔中提到了以下方法:HMR - 接受多個依賴

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

在回答這個拍攝一個鏡頭,因爲沒有其他人嘗試過。

從看你的代碼看起來很好。

回調方法在接受多個依賴關係後運行。 您基本上發送一個函數作爲參數,然後一旦所有的依賴被接受,該函數就會被執行。

所以你幾乎可以放任何你喜歡的功能。例如:

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'參數上以查看它是否包含任何內容 - if它確實如此,你可以相應地處理這些數據。

希望這會有所幫助。