使用monaco編輯器 報錯Can only have one anonymous define call per script file

web-小爽發表於2020-10-30


問題描述:

開啟monaco編輯器後,再開啟含有amd載入方式的js檔案時,報錯 Can only have one anonymous define call per script file


原因分析:

這是因為monaco的資原始檔中有個判斷,不能一個專案中有兩個define的定義函式,因為monaco需要自己的定義。所以避免define方法互相汙染而報錯,或者資原始檔沒載入!


解決方案:

在create之後 加上 

if(editor && "function" == typeof define && define.amd) {

    delete define.amd;

 }

雖然刪掉了amd屬性,對monaco也不會有影響。

我們看下下面webpack編譯後的檔案,這樣再載入這個檔案就不會走到define 的判斷裡面,所以不會再出現Can only have one anonymous define call per script file 這個報錯

相關文章