[英]How can I change my own library's index.js to import it as local object, not as global one?
Currently I have index.js file in my library. 目前,我的库中有index.js文件。
import method1 from './method1';
import method2 from './method2';
(function(window){
window.myLib = {
processArray,
processString,
}
})(window);
And if I wanna use this library, I should do like: 如果我想使用该库,我应该这样做:
import 'my-lib';
myLib.method1();
How can I refactor my index.js to not use global object and use this library like: 我如何重构我的index.js以不使用全局对象,并像下面这样使用该库:
import myLyb from 'my-lib';
myLib.method1();
I did this for a custom library of my own. 我这样做是为了自己的自定义库。
Using the modern Class syntax, assuming this file was called MyLib.js
: 使用现代的Class语法,假设此文件名为MyLib.js
:
class MyLib {
constructor() {
// do stuff
}
// other functions that do stuff
processArray() {
}
processString() {
}
}
export default MyLib;
In your other code, such as index.js
, you'd import and use your library like this: 在其他代码(例如index.js
,您将像这样导入和使用您的库:
import MyLib from './MyLib'
// later on
MyLib.processArray()
MyLib.processString()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.