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.dev.org.tw) 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();

您找到需要的資訊了嗎?

隱私權政策