簡體   English   中英

IONIC 4+ 電容器:如何在沒有離子原生的情況下從 github 安裝 cordova 插件(Stepcounter)?

[英]IONIC 4+ Capacitor: How to install a cordova plugin (Stepcounter) from github without ionic native?

我想將 GitHub 上可用的Cordova插件添加到我的 IONIC 5+ 電容器(角)項目中。

另外,我不知道如何安裝和集成這個插件,因為官方手冊說在步驟npm install https://github.com/DigitalsunrayMedia/cordova-plugin-stepcounter之后還有npm install ionic-native/???????

我的問題就在這里! npm install ionic-native/???????? 進入? 所需的插件不作為 Ionic Native 插件存在。

如果我只是執行以下操作就足夠了:

npm install https://github.com/DigitalsunrayMedia/cordova-plugin-stepcounter.git npx cap sync

沒有npm install ionic-native/????

我還想知道我是否可以在 Ionic Capacitor 中輕松添加和使用它,或者我是否必須在文件中進行更改。

如何解決 Typescript 中的這個插件? 我必須向 module.app 添加任何內容嗎?

如果我按照 Capacitor 規定的方式進行操作是否足夠: import { Plugins } from '@capacitor/core'; const { Stepcounter } = Plugins;

我非常感謝任何建議:謝謝,)最好的問候,程序員

是的,您可以安裝插件並在沒有 ionic-native 的情況下使用它,基本上 ionic-native 只是庫的類型化包裝器。

最簡單的方法是實現服務

import { Injectable } from '@angular/core';

declare var stepcounter: any;

@Injectable({
    providedIn: 'root'
})
export class StepCounterService {
    constructor() {}

    start(startingOffset) {
        return new Promise((resolve, reject) => {
            stepcounter.start(
                startingOffset,
                message => {
                    resolve(message);
                },
                () => {
                    reject();
                }
            );
        });
    }

    stop() {
        return new Promise((resolve, reject) => {
            stepcounter.stop(
                message => {
                    resolve(message);
                },
                () => {
                    reject();
                }
            );
        });
    }

    getTodayStepCount() {
        return new Promise((resolve, reject) => {
            stepcounter.getTodayStepCount(
                message => {
                    resolve(message);
                },
                () => {
                    reject();
                }
            );
        });
    }

    getStepCount() {
        return new Promise((resolve, reject) => {
            stepcounter.getStepCount(
                message => {
                    resolve(message);
                },
                () => {
                    reject();
                }
            );
        });
    }

    deviceCanCountSteps() {
        return new Promise((resolve, reject) => {
            stepcounter.deviceCanCountSteps(
                message => {
                    resolve(message);
                },
                () => {
                    reject();
                }
            );
        });
    }

    getHistory() {
        return new Promise((resolve, reject) => {
            stepcounter.getHistory(
                message => {
                    resolve(message);
                },
                () => {
                    reject();
                }
            );
        });
    }
}

現在你把它注入你需要的地方,這樣你就可以使用它了

PS。 我假設您使用的是 angular 和 typescript 如果您使用的是香草離子和 javascript 您可以安裝插件並使用它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM