deno.com

导入 & 导出

在 Github 上编辑

为了构建可组合的程序,必须能够从其他模块导入和导出函数。这通过在 Deno 中使用 ECMA script 模块来实现。

要导出函数,请使用 export 关键字。
./util.ts
export function sayHello(thing: string) {
  console.log(`Hello, ${thing}!`);
}
您还可以导出类型、变量和类。
./util.ts
export interface Foo {}
export class Bar {}
export const baz = "baz";
要从其他文件导入内容,可以使用 import 关键字。
./main.ts
import { sayHello } from "./util.ts";
sayHello("World");
您还可以导入文件中的所有导出项。
./main.ts
import * as util from "./util.ts";
util.sayHello("World");
导入不必是相对的,它们也可以引用绝对文件、https、npm 或 [JSR](https://jsr.deno.org.cn) URL。
./main.ts
import { camelCase } from "jsr:@luca/cases@1";
console.log(camelCase("hello world")); // helloWorld

import OpenAI from "jsr:@openai/openai";
const client = new OpenAI();

您找到所需的内容了吗?

隐私政策