如何将Cordova插件公开到Svelte App?

问题描述

我有一个带有svelte的cordova应用,但是我在将cordova.plugins从cordova暴露到svelte时遇到了问题

这是我尝试过的 index.js

const app = {


// Application Constructor
  initialize: function () {
    this.bindEvents();
  },bindEvents: function () {
    document.addEventListener("deviceready",this.onDeviceReady,false);
    document.addEventListener("offline",this.onDeviceOffline,false);
  },onDeviceOffline: function () {
    app.receivedEvent("No Internet");
    PHONEGAP.isOffline = true;
  },onDeviceReady: function () {
    app.receivedEvent("deviceready");
    console.log(cordova.plugins); // this return object plugins
    if (cordova && cordova.plugins) {
      PHONEGAP.plugins = cordova.plugins;
      PHONEGAP.platform = cordova.platformId;
    }
},cordovaObject: function () {
    return cordova;
  },// Update DOM on a Received Event
  receivedEvent: function (id) {
    console.log("Received Event: " + id);
  },};

index.html

<script type="text/javascript" src="cordova.js"></script>
<script>
  let _showSplash = localStorage.getItem('init');
  let _lNotif = localStorage.getItem("notification");
  let _lUnreadMessage = localStorage.getItem("messages_unread");

  if(_showSplash){
    if(parseInt(_showSplash) == 0){
      localStorage.removeItem('init');
    }
  }else{
    localStorage.setItem('init',1);
  }
</script>

<script>
var PHONEGAP = {
        plugins: false,platform: false,isOffline: false,messaging: false,notification: _lNotif ? JSON.parse(localStorage.getItem("notification")) : []
};
</script>
<script type="text/javascript" src="js/index.js"></script>
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.17.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.17.1/firebase-messaging.js"></script>
<script type="text/javascript">
    
    app.initialize();
    console.log(cordova.plugins); //this return **undefined**


  
</script>

<script src="js/main.bundle.js"></script>

我是Cordova的新手,所以我不明白为什么我不能在我的Svelte应用程序(main.bundle.js)中调用cordova.plugins。

我还尝试通过调用console.log(cordova)在svelte应用程序中调用cordova对象,它可以正常工作!我检查了控制台,它的内部有plugins对象,但是当我调用console.log(cordova.plugins)时,它返回未定义的内容。

cordova version 6.0.0
platformVesion 6.0.0
platformId browser (it has same error too with android platform)
phonegap cli version 9.0.0

phonegap plugins

的结果
cordova-plugin-androidx 2.0.0 "cordova-plugin-androidx"
cordova-plugin-androidx-adapter 1.1.1 "cordova-plugin-androidx-adapter"
cordova-plugin-battery-status 1.2.5 "Battery"
cordova-plugin-camera 2.4.1 "Camera"
cordova-plugin-compat 1.2.0 "Compat"
cordova-plugin-device 1.1.7 "Device"
cordova-plugin-dialogs 1.3.4 "Notification"
cordova-plugin-file 4.3.3 "File"
cordova-plugin-firebase-analytics 4.3.1 "FirebaseAnalyticsPlugin"
cordova-plugin-firebase-messaging 4.4.2 "FirebaseMessagingPlugin"
cordova-plugin-geolocation 2.4.3 "Geolocation"
cordova-plugin-globalization 1.0.9 "Globalization"
cordova-plugin-inappbrowser 1.7.2 "InAppBrowser"
cordova-plugin-media-capture 1.4.3 "Capture"
cordova-plugin-network-information 1.3.4 "Network Information"
cordova-plugin-vibration 2.1.6 "Vibration"
cordova-plugin-whitelist 1.3.4 "Whitelist"
cordova-support-android-plugin 1.0.2 "cordova-support-android-plugin"
cordova-support-google-services 1.4.1 "cordova-support-google-services"

console.log(cordova)

的结果
 {version: "6.0.0",platformVersion: "6.0.0",platformId: "browser",define: ƒ,require: ƒ,…}
    InAppBrowser: {open: ƒ}
    addConstructor: ƒ (func)
    addDocumentEventHandler: ƒ (event)
    addStickyDocumentEventHandler: ƒ (event)
    addWindowEventHandler: ƒ (event)
    callbackError: ƒ (callbackId,args)
    callbackFromNative: ƒ (callbackId,isSuccess,status,args,keepCallback)
    callbackId: 1855496235
    callbackStatus: {NO_RESULT: 0,OK: 1,CLASS_NOT_FOUND_EXCEPTION: 2,ILLEGAL_ACCESS_EXCEPTION: 3,INSTANTIATION_EXCEPTION: 4,…}
    callbackSuccess: ƒ (callbackId,args)
    callbacks: {}
    commandProxy: {add: ƒ,remove: ƒ,get: ƒ}
    define: ƒ (id,factory)
    exec: ƒ (success,fail,service,action,args)
    file: {applicationDirectory: "http://localhost:3000/",applicationStorageDirectory: null,dataDirectory: "filesystem:file:///persistent/",cacheDirectory: "filesystem:file:///temporary/",externalApplicationStorageDirectory: null,…}
    fireDocumentEvent: ƒ (type,data,bNoDetach)
    fireWindowEvent: ƒ (type,data)
    getOriginalHandlers: ƒ ()
    platformId: "browser"
    platformVersion: "6.0.0"
    plugins: {firebase: {…}}
    removeDocumentEventHandler: ƒ (event)
    removeWindowEventHandler: ƒ (event)
    require: ƒ (id)
    version: "6.0.0"
    __proto__: Object

修改 这里的问题是当我调用console.log(cordova)时,结果对象内部有 plugins 对象,但是当我调用console.log(cordova.plugins)时,它返回 undefined (所有在主index.html文件中调用)。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)