简体   繁体   English

将 Ionic Native / Cordova Plugins 与 Ionic (React) & Capacitor 一起使用的正确方法是什么?

[英]What is the right way to use Ionic Native / Cordova Plugins with Ionic (React) & Capacitor?

How can I use Ionic Native / Cordova Plugins with Ionic React (not Angular) using Capacitor instead of Cordova?如何使用 Capacitor 而不是 Cordova 将 Ionic Native/Cordova 插件与 Ionic React(不是 Angular)一起使用?

I want to make use of Screen Orientation Plugin with Capacitor and Ionic React.我想将Screen Orientation Plugin与 Capacitor 和 Ionic React 结合使用。 This is because Cordova is not officially supported for Ionic React projects.这是因为 Ionic React 项目不正式支持 Cordova。 Know More .了解更多

This is what I have tried.这是我尝试过的。

import { ScreenOrientation } from '@ionic-native/screen-orientation'
.
.
.
.
useEffect(() => {
    ScreenOrientation.lock(ScreenOrientation.ORIENTATIONS.LANDSCAPE)
}, [])

My package.json has the following relevant dependencies installed.我的 package.json 安装了以下相关依赖项。

"@ionic-native/core": "^5.13.0",
"@ionic-native/screen-orientation": "^5.13.0",
"cordova-plugin-screen-orientation": "^3.0.2"

The application builds successfully, but still opens in Portrait mode on my Android device.应用程序构建成功,但仍以纵向模式在我的 Android 设备上打开。 If I try to display the following,如果我尝试显示以下内容,

console.log(ScreenOrientation.type)

The app builds successfully, but the screen goes blank.应用程序构建成功,但屏幕变为空白。

Capacitor's Native APIs work flawlessly with Ionic React. Capacitor 的原生 API 与 Ionic React 完美配合。 For example,例如,

import { Plugins } from '@capacitor/core'
const { StatusBar } = Plugins
.
.
useEffect(() => {
    StatusBar.hide()
}, [])

Will hide the status bar on my Android device.将隐藏我的 Android 设备上的状态栏。

Is this the right approach for using Cordova Plugins with Capacitor in Ionic React?这是在 Ionic React 中使用带有电容器的 Cordova 插件的正确方法吗? Reference 参考

If yes, what am I doing wrong here?如果是,我在这里做错了什么? And if no, how can I achieve the same?如果不是,我怎样才能达到同样的目标?

Problem solved!问题解决了! I was doing everything right, I had just forgotten to run npx cap sync after installing my Ionic Native Plugin.我做的一切都是正确的,我只是在安装我的 Ionic Native Plugin 后忘记运行npx cap sync

So, the right way to use Cordova Plugins with Capacitor & Ionic (React) is因此,将 Cordova 插件与 Capacitor & Ionic (React) 一起使用的正确方法是

npm install @ionic-native/javascript-package-name
npm install cordova-plugin-name
npx cap sync

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM