語法
js
addModule(moduleURL)
addModule(moduleURL, options)
引數
moduleURL-
一個
String,包含要新增的模組所在的 JavaScript 檔案的 URL。 options可選-
包含以下選項之一的物件:
credentials-
一個
Request.credentials值,指示在載入模組時是否傳送憑據(例如,cookie 和 HTTP 認證)。可以是"omit"、"same-origin"或"include"之一。預設為"same-origin"。另請參閱Request.credentials。
返回值
一個 Promise,在給定 URL 的模組新增完成後解析。此 promise 不返回任何值。
異常
如果 addModule() 失敗,它會拒絕 promise,並向拒絕處理程式提供以下錯誤之一。
AbortErrorDOMException-
指定的指令碼無效或無法載入。
SyntaxErrorDOMException-
指定的
moduleURL無效。
示例
AudioWorklet 示例
js
const audioCtx = new AudioContext();
const audioWorklet = audioCtx.audioWorklet;
audioWorklet.addModule("modules/bypassFilter.js", {
credentials: "omit",
});
PaintWorklet 示例
js
CSS.paintWorklet.addModule(
"https://mdn.github.io/houdini-examples/cssPaint/intro/worklets/hilite.js",
);
一旦指令碼被新增到 paint worklet,CSS paint() 函式就可以用來包含由 worklet 建立的影像。
css
@supports (background-image: paint(id)) {
h1 {
background-image: paint(hollow-highlights, filled, 3px);
}
}
規範
| 規範 |
|---|
| HTML # dom-worklet-addmodule-dev |
瀏覽器相容性
載入中…