下面是带有事件监听器的 Vue 组件类,它可以在某个时间点接收服务工作者的实例并将其保存在registration
属性下。
我遇到的问题是,即使这样做:
if (this.registration && this.registration.waiting) {
this.registration.waiting.postMessage(SERVICE_WORKER_MESSAGES.skipWaiting)
}
TypeScript 传达这this.registration
是TS2571: Object is of type 'unknown'.
有道理的,因为:
private registration: unknown
如果有人能建议什么是初始化和访问稍后可以定义的属性的正确方法,我将不胜感激?
PS:实际的类型registration
是ServiceWorkerRegistration
类实现:
export default class Core extends Vue {
private refreshing = false
private updateExists = false
private registration: unknown
updateApp (): void {
this.updateExists = false
// this.registration --> TS2571: Object is of type 'unknown'.
if (this.registration && this.registration.waiting) {
// ~~~~~~~~~~~~~~~~~
// this.registration --> TS2571: Object is of type 'unknown'.
this.registration.waiting.postMessage(SERVICE_WORKER_MESSAGES.skipWaiting)
// ~~~~~~~~~~~~~~
}
}
addEventListeners () {
document.addEventListener(SERVICE_WORKER_EVENTS.updated, ((event: CustomEvent) => {
this.registration = event.detail /* store the ServiceWorkerRegistration instance for later use. */
this.updateExists = true
}) as EventListener,
{ once: true })
}
created () {
this.addEventListeners()
/*
Refresh all open app tabs when a new service worker is installed */
navigator.serviceWorker.addEventListener(`controllerchange`, () => {
if (!this.refreshing) {
this.refreshing = true
window.location.reload()
}
})
}
}
tsconfig.json
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"allowJs": true,
"sourceMap": true,
"noImplicitThis": true,
"baseUrl": ".",
"types": [
"webpack-env",
"jest"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules"
]
}