首页 > 解决方案 > 电容器插件在 android build 上显示为未定义

问题描述

这是我找到的一个电容器插件https://github.com/JhonArlex/capacitor_qrcode 我希望它将它集成到我的 ionic 应用程序中,在 web 服务上它按预期工作,但是当我在 android 上尝试 livereload 时,错误屏幕弹出出来并说插件未定义..

import "capacitor_qrcode";
import { Plugins } from "@capacitor/core";

//..

await Plugins.QRCodePlugin.getCodeQR();
// QRCodePlugin is undefined?

我正在使用 Ionic React Capacitor ...如果您能提出任何其他方式我可以在我的应用程序中集成 QR 码扫描功能,我将不胜感激!谢谢!

标签: reactjsionic-frameworkcapacitorionic-react

解决方案


使用您自己的插件时,您需要将其注册/添加到您的 android MainActivity 中。 https://capacitor.ionicframework.com/docs/plugins/android#export-to-capacitor

像这样:

import com.jhon.capacitor_qrcode.QRCodePlugin;

public class MainActivity extends BridgeActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initializes the Bridge
    this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
      // Additional plugins you've installed go here
      // Ex: add(TotallyAwesomePlugin.class);
      add(QRCodePlugin.class);
    }});
  }
}

推荐阅读