當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js import.meta.resolve(specifier[, parent])用法及代碼示例

import.meta.resolve(specifier[, parent])

Stability: 1 - 實驗性

此函數僅在啟用--experimental-import-meta-resolve 命令標誌時可用。

  • specifier <string> 相對於 parent 解析的模塊說明符。
  • parent <string> | <URL> 要解析的絕對父模塊 URL。如果未指定,則使用 import.meta.url 的值作為默認值。
  • 返回: <Promise>

提供範圍為每個模塊的module-relative 解析函數,返回 URL 字符串。

const dependencyAsset = await import.meta.resolve('component-lib/asset.css');

import.meta.resolve 還接受第二個參數,它是要從中解析的父模塊:

await import.meta.resolve('./dep', import.meta.url);

這個函數是異步的,因為 Node.js 中的 ES 模塊解析器是允許異步的。

相關用法


注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 import.meta.resolve(specifier[, parent])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。