Ant*_*ony 3 javascript service-worker visual-studio-code
为什么 vscode 看不到 Service Worker 接口对象?
\n缺乏自动完成
\n 虽然浏览器显示它们仍然存在
\n存在登录在浏览器中的对象
\n如何解决此问题?
整个服务人员代码:
\n\nself.addEventListener(\'install\', event => {\n console.log(\'V1 installing\xe2\x80\xa6\');\n event.waitUntil(\n caches.open(\'static-v1\').then(cache => cache.add(\'/cat.png\'))\n );\n});\n\nself.addEventListener(\'activate\', event => {\n console.log(\'V1 now ready to handle fetches!\');\n});\n\nself.addEventListener(\'fetch\', event => {\n const url = new URL(event.request.url);\n if (url.origin == location.origin && url.pathname == \'/dog.png\') {\n event.respondWith(caches.match(\'/cat.png\'));\n }\n\n if (url.origin == location.origin) {\n console.log("Clients object: ", clients);\n }\n});\nRun Code Online (Sandbox Code Playgroud)\n
最简单的方法是使用 JSDOC 语法
/**@type {ServiceWorkerGlobalScope} sw */
const sw = self
Run Code Online (Sandbox Code Playgroud)
并在代码中使用该常量。
sw.addEventListener('install', (ev) => {
// the event argument will have ServiceWorker specific intellisense
ev.wait
})
Run Code Online (Sandbox Code Playgroud)
排除默认库,例如与“webworker”冲突的“dom”
在文件顶部添加
/// <reference no-default-lib="true"/>
/// <reference lib="esnext" />
/// <reference lib="webworker" />
Run Code Online (Sandbox Code Playgroud)