[英]How do you find out which functions are exported by a node/npm package?
Following my earlier question , and the Mozilla documentation on import
, I now understand that I must do something like the following to use the functionality in a module:在我之前的问题和关于
import
的 Mozilla 文档之后,我现在明白我必须执行以下操作才能使用模块中的功能:
import * as name from "module";
orimport {functionName} from "module";
Coming from using CommonJS, I never thought about which functions were exported by a package because I just used to require
them like:来自使用 CommonJS,我从没想过 package 导出了哪些函数,因为我曾经像这样
require
它们:
const vueServerRenderer = require('vue-server-renderer') // get the module
vueServerRenderer.createRenderer() // use a function in that module
How can someone find out which functions are being exported by a module such as express
or vueServerRenderer
so I know how to use the correct import
statement like:有人如何找出诸如
express
或vueServerRenderer
之类的模块正在导出哪些函数,所以我知道如何使用正确的import
语句,例如:
import express from 'express'
instead of import * as express from 'express'
? import express from 'express'
而不是import * as express from 'express'
?
You need to read the module source.您需要阅读模块源代码。
Every export
statement exports something.每个
export
语句都会导出一些东西。 It may be a function, an array, a string, a class etc.它可能是 function、数组、字符串、class 等。
Every export
statement without default
needs to be destructured on import:每个没有
default
的export
语句都需要在导入时进行解构:
import { NonDefaultThing1, NonDefaultThing2 } from 'somewhere'
An export
statement with default
must be imported directly without the {}
:必须直接导入具有
default
的export
语句,而无需{}
:
import DefaultThing from 'somewhere'
Some modules have default export but also non-default exports.一些模块有默认导出但也有非默认导出。 You can pick and choose what to import:
您可以选择要导入的内容:
import DefaultThing, { NonDefaultThing7 } from 'somewhere'
If you use an IDE that can parse javascript such as Microsoft Visual Studio Code you can get autocompletion/intellisense of the import
statement.如果您使用可以解析 javascript 的 IDE(例如 Microsoft Visual Studio Code ),您可以获得
import
语句的自动完成/智能感知。 There are even plugins that does auto-import: just use a class or function or something from a module and it will automatically add the required import statement at the top of your file.甚至还有自动导入的插件:只需使用 class 或 function 或来自模块的东西,它会自动在文件顶部添加所需的导入语句。
TLDR: The default
export. TLDR:
default
导出。
Say a particular library named "module" has the following code假设一个名为“模块”的特定库具有以下代码
function functionName() {
// function body
}
export default functionName;
Now, in your code, if you put现在,在你的代码中,如果你把
import blah from "module";
then blah
will point to functionName
.然后
blah
将指向functionName
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.