实现 Web/PWA 端功能
在设计插件 API 时,我们发现 Web 平台已经原生支持屏幕方向功能(当然,移动设备除外)。你可能会有疑问:为什么我们的插件还需要实现 Web 端?难道不能通过编程判断用户是否在 Web 端,然后直接使用 Screen Orientation Web API,否则再使用插件吗?
"一次编写,随处运行"是 Web 原生应用的核心理念,这一理念同样适用于插件开发。使用 Capacitor 插件的开发者应该能够使用相同的插件类和方法,并确保它们在所有平台上都能正常工作。
因此,作为负责任的开发者,我们将把 Screen Orientation Web API 封装到 ScreenOrientation
插件的 Web 端实现中。
扩展 Capacitor 的 WebPlugin 类
新建文件 src/plugins/screen-orientation/web.ts
,这里将编写 ScreenOrientation
插件的 Web 端实现。
首先声明 ScreenOrientationWeb
类并继承 WebPlugin
:
import { WebPlugin } from '@capacitor/core';
import type { ScreenOrientationPlugin } from './definitions';
export class ScreenOrientationWeb extends WebPlugin {
constructor() {
super();
}
}