[英]How can I create a javascript library in a separate file and “include” it in another?
[英]Trying to create a separate file I can import in JavaScript with "new" involved
我有一個對象,緩存聲明如下:
const cache = new InMemoryCache({);
我想創建一個單獨的文件,我可以配置傳入 InMemoryCache({...}) 的所有參數。 當我創建一個名為 cache.js 的新文件時,如下所示,
export const cache = new InMemoryCache();
然后從我的郵件文件中,將其導入為`import {cache} from './cache.js'
我從我的原始代碼中得到了不同的行為。 我在想我沒有得到這里的新東西。 建議?
new
用於創建對象的新實例(在本例中為InMemoryCache
)。
如果要導出使用定義的單個實例
const cache = new InMemoryCache({});
您需要將導出作為
export { cache };
這將導出之前創建的實例。
如果您還希望能夠從其他地方創建一個新實例,您可以使用以下命令導出構造函數
export { InMemoryCache };
(您可以將兩個導出合並在一起)
然后你可以稍后導入cache
(已經定義的實例)和InMemoryCache
(構造函數)使用
import { cache, InMemoryCache } from '<FILENAME>';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.