【问题标题】:How can I "wrap" a capacitor plugin in React so that I can manage it centrally?我如何在 React 中“包装”一个电容器插件,以便我可以集中管理它?
【发布时间】:2022-12-03 23:46:35
【问题描述】:

今天,Capacitor 发布了一个新版本,将 @capacitor-community/http 插件替换为 CapacitorHttp 插件。

我正在为我的应用程序使用 Ionic React,所以我的所有代码都在 TypeScript/React 中。

我想测试新的 CapacitorHttp 插件,但我也想在不成功时轻松恢复使用 http 插件。

在我的整个应用程序中,我有很多导入语句,例如 import { Http } from '@capacitor-community/http';。在 React 中,有没有办法以某种方式包装此导入,以便我可以在任何地方导入 MyCustomHttp,然后在 MyCustomHttp 中导入 CapacitorHttphttp?这将通过将导入放在一个地方来简化我的代码,但我不知道该怎么做。

【问题讨论】:

  • 创建您自己的文件并执行export { CapacitorHttp as Http } from '@capacitor/core';

标签: reactjs capacitor capacitor-plugin


【解决方案1】:

正如@Konrad 所评论的,您可以在自定义文件中使用{ A as B }。以下是 Capacitor Push Notifications 插件的代码示例:

插件推送通知.ts

import {
  ActionPerformed,
  PushNotifications,
  Token,
} from '@capacitor/push-notifications';

export type { ActionPerformed as PluginActionPerformed };

export type { Token as PluginToken };

export { PushNotifications as PluginPushNotifactions };

为了帮助我自己使用 IDE 的自动完成功能,我重新导出了带有 Plugin 前缀的所有内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 2015-03-11
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 2020-11-19
    相关资源
    最近更新 更多