參考:http://www.cnblogs.com/jfw10973/p/3921899.html
https://github.com/zeroclipboard/zeroclipboard
近期該項目引入了Requirejs,結果發現在有富文本編輯器的頁面都會在控制台報出如下異常:
Uncaught ReferenceError: ZeroClipboard is not defined ueditor.all.min.js:265
經查看代碼后發現 ueditor.../third-party/zeroclipboard/ZeroClipboard.js中 輸出方法的地方是醬紫的
if (typeof define === "function" && define.amd) {
define(function() {
return ZeroClipboard;
});
} else if (typeof module === "object" && module && typeof module.exports === "object" && module.exports) {
module.exports = ZeroClipboard;
} else {
window.ZeroClipboard = ZeroClipboard;
}
意思就是說
如果當前頁面的模塊加載模式是AMD的 則定義模塊
如果是CommonJs的,則輸出到模塊 ZeroClipboard
否則 把 ZeroClipboard 定義為全局變量
這樣 解決方案就有兩種。
①不使用模塊加載模式來使用這個功能
這樣方法需要修改一點源碼,把上面這段代碼替換成如下代碼即可
if (typeof define === "function" && define.amd) {
define(function() {
return ZeroClipboard;
});
} else if (typeof module === "object" && module && typeof module.exports === "object" && module.exports) {
module.exports = ZeroClipboard;
}
window.ZeroClipboard = ZeroClipboard;
②如果不修改源碼,就得在模塊加載時做處理了
首先是修改配置
require.config({
baseUrl: '',
paths: {
ZeroClipboard: "./UEditor.../ZeroClipboard"//主要是加這句話
}
});
然后是在調用這個模塊並把模塊定義到全局變量
require(['ZeroClipboard'], function (ZeroClipboard) {
window['ZeroClipboard'] = ZeroClipboard;
});
