在NodeJS中,有没有一种方式或模式可以只在一个包的模块中共享函数,而不允许从另一个包中共享它们?
例如,如果包A包含file1.js、file2.js和index.js。index.js使用file1和file2中的函数。
包B使用包A。似乎从file1和file2导出的所有模块也可用于包B。是否仅限于从包A的index.js导出的模块?
简而言之,是否支持像受保护的作用域这样的东西?
发布于 2020-09-27 10:57:11
正如AZ_所说:只导出您想导出的内容。
示例:
// file1
export const foo = () => { console.log("foo"); }
// file2
import {foo} from "./file1"
export const bar = () => {
foo();
console.log("bar");
}
// index
import {bar} from "./bar"
// the package will only export bar
export { bar }
https://stackoverflow.com/questions/60026602
复制