gpt4 book ai didi

Angular 7 - HTML5 全屏 API 如何工作?我有很多错误

转载 作者:行者123 更新时间:2023-12-02 16:40:41 32 4
gpt4 key购买 nike

我使用 Angular 7,我希望有一个按钮可以将我的应用程序置于全屏模式。我使用 HTML5 Fullscreen API 并且创建了 2 个函数:

openfullscreen() {
// Trigger fullscreen
console.log('gg');
if (document.documentElement.requestFullscreen) {
document.documentElement.requestFullscreen();
} else if (document.documentElement.mozRequestFullScreen) { /* Firefox */
document.documentElement.mozRequestFullScreen();
} else if (document.documentElement.webkitRequestFullscreen) { /* Chrome, Safari and Opera */
document.documentElement.webkitRequestFullscreen();
} else if (document.documentElement.msRequestFullscreen) { /* IE/Edge */
document.documentElement.msRequestFullscreen();
}
this.isfullscreen = true;
}

closefullscreen(){
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) { /* Firefox */
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) { /* Chrome, Safari and Opera */
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) { /* IE/Edge */
document.msExitFullscreen();
}
this.isfullscreen = false;
}

一开始它有效,但我遇到了很多错误:

error TS2339: Property 'mozRequestFullScreen' does not exist on type 'HTMLElement'.

error TS2339: Property 'mozRequestFullScreen' does not exist on type 'HTMLElement'.

error TS2339: Property 'webkitRequestFullscreen' does not exist on type 'HTMLElement'.

error TS2339: Property 'webkitRequestFullscreen' does not exist on type 'HTMLElement'.

error TS2551: Property 'msRequestFullscreen' does not exist on type 'HTMLElement'. Did you mean 'requestFullscreen'?

error TS2551: Property 'msRequestFullscreen' does not exist on type 'HTMLElement'. Did you mean 'requestFullscreen'?

error TS2339: Property 'mozCancelFullScreen' does not exist on type 'Document'.

error TS2339: Property 'mozCancelFullScreen' does not exist on type 'Document'.

error TS2339: Property 'webkitExitFullscreen' does not exist on type 'Document'.

error TS2339: Property 'webkitExitFullscreen' does not exist on type 'Document'.

error TS2551: Property 'msExitFullscreen' does not exist on type 'Document'. Did you mean 'exitFullscreen'?

error TS2551: Property 'msExitFullscreen' does not exist on type 'Document'. Did you mean 'exitFullscreen'?

当我重新启动代码时,我遇到了同样的错误,但除此之外,我还有:

Failed to compile.

我的应用程序无法运行。如何编译才不会出错?

最佳答案

您可以通过使用 as 关键字来转换 documentdocument.documentElement< 的接口(interface),告诉 typescript 您将要使用的方法.

像这样:

const docElmWithBrowsersFullScreenFunctions = document.documentElement as HTMLElement & {
mozRequestFullScreen(): Promise<void>;
webkitRequestFullscreen(): Promise<void>;
msRequestFullscreen(): Promise<void>;
};

const docWithBrowsersExitFunctions = document as Document & {
mozCancelFullScreen(): Promise<void>;
webkitExitFullscreen(): Promise<void>;
msExitFullscreen(): Promise<void>;
};

请注意,这只是防止编译错误,您仍然应该像您一样检查方法是否存在。

所以你的方法将是这样的:

openfullscreen() {
// Trigger fullscreen
const docElmWithBrowsersFullScreenFunctions = document.documentElement as HTMLElement & {
mozRequestFullScreen(): Promise<void>;
webkitRequestFullscreen(): Promise<void>;
msRequestFullscreen(): Promise<void>;
};

if (docElmWithBrowsersFullScreenFunctions.requestFullscreen) {
docElmWithBrowsersFullScreenFunctions.requestFullscreen();
} else if (docElmWithBrowsersFullScreenFunctions.mozRequestFullScreen) { /* Firefox */
docElmWithBrowsersFullScreenFunctions.mozRequestFullScreen();
} else if (docElmWithBrowsersFullScreenFunctions.webkitRequestFullscreen) { /* Chrome, Safari and Opera */
docElmWithBrowsersFullScreenFunctions.webkitRequestFullscreen();
} else if (docElmWithBrowsersFullScreenFunctions.msRequestFullscreen) { /* IE/Edge */
docElmWithBrowsersFullScreenFunctions.msRequestFullscreen();
}
this.isfullscreen = true;
}

closefullscreen(){
const docWithBrowsersExitFunctions = document as Document & {
mozCancelFullScreen(): Promise<void>;
webkitExitFullscreen(): Promise<void>;
msExitFullscreen(): Promise<void>;
};
if (docWithBrowsersExitFunctions.exitFullscreen) {
docWithBrowsersExitFunctions.exitFullscreen();
} else if (docWithBrowsersExitFunctions.mozCancelFullScreen) { /* Firefox */
docWithBrowsersExitFunctions.mozCancelFullScreen();
} else if (docWithBrowsersExitFunctions.webkitExitFullscreen) { /* Chrome, Safari and Opera */
docWithBrowsersExitFunctions.webkitExitFullscreen();
} else if (docWithBrowsersExitFunctions.msExitFullscreen) { /* IE/Edge */
docWithBrowsersExitFunctions.msExitFullscreen();
}
this.isfullscreen = false;
}

关于Angular 7 - HTML5 全屏 API 如何工作?我有很多错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54242775/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com