簡體   English   中英

Node.js module.export 與導出緩存

[英]Node.js module.export vs export caching

在瀏覽了節點文檔后,我對緩存有了一點了解。 使用module.exports{constant_1, constant_2}將緩存其分配的值,因此每次我們調用 require('') 時,它都會獲取緩存的值而不是創建一個新值。

example_1.js
const test = 'testValue';
module.export = {test};

example_2.js
const cachedValue = require('../example_1.js');
console.log(cachedValue);

但是當我們使用以下語法時會發生緩存嗎? 也導出{constant_1, constant_2}語句?

example_1.js
const test = 'testValue';
export {test};

example_2.js
import {test} from "example_1";
console.log(test);
import {test} from "example_1";

應該

import {test} from "./example_1.js";

類似於 CommonJS 示例,但除此之外,ES 模塊的緩存行為類似。 實例狀態的ESM 文件節點文檔

ES 模塊被解析並緩存為 URL。 這意味着文件包含特殊字符,如 # 和 ? 需要逃脫。

因此,只要./example_1.js和其他地方的另一個解析為相同的 URL,它就會被緩存為相同的模塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM