Web/PWA 平台实现
在设计插件 API 时,我们发现 Web 平台已经支持屏幕方向功能(当然,移动设备除外)。您可能会问,为什么我们的插件还需要 Web 实现……难道不能通过编程方式检测用户是否在 Web 端,然后使用 Screen Orientation Web API,否则就使用插件吗?
Web Native 应用程序的理念是“一次编写,随处运行”。这同样适用于插件;使用 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();
}
}