为什么cordova-plugin-nativestorage在浏览器上起作用但在电话上不起作用 [英] Why does cordova-plugin-nativestorage work on browser but not on phone

查看:101
本文介绍了为什么cordova-plugin-nativestorage在浏览器上起作用但在电话上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Phonegap CLI服务我的项目,我的插件版本是:2.3.1 因此,我编写了以下代码以在设备就绪后执行:

I am using the Phonegap CLI to serve my project and my plugin version is: 2.3.1 So, I wrote the following code to execute after the device is ready:

NativeStorage.getItem("abcd",function(){
			console.log("success");alert("success");
		},function(){
			console.log("fail");alert("failed :)");
		});

当我在浏览器上对其进行测试时,此方法非常理想.但是,当我在Android手机上打开此应用程序时,NativeStorage代码根本不起作用.

This works perfectly when I am testing it on my browser. However, when I open this app on my android phone, the NativeStorage code does not work at all.

我用weinre调试了我的应用程序:

I used weinre to debug my app:

我收到错误:ReferenceError:未定义NativeStorage

I got the error: ReferenceError: NativeStorage is not defined

我还删除了插件和所有平台,然后再次重新安装;但是,我仍然遇到相同的错误.

I also removed the plugin and all the platforms and reinstalled them again; however, I am still getting the same error.

能帮我找到问题吗?

推荐答案

在调用NativeStorage插件之前,您是否正在等待设备准备就绪?

在index.js中,您应该添加类似于以下代码的设备处理内容.

In your index.js you should add something like the following code for the device handling.

if (window.cordova) {
  document.addEventListener("deviceready", onDeviceReady, false);
}

注意:如果要在没有Cordova容器的浏览器中使用应用程序,则应定义一个else分支.如果不是Cordova容器,它将跳到else分支并立即执行功能.

Note: If you want to use your app in the browser without the Cordova container, you should define an else branch like this. If its not a Cordova container, it will jump to the else branch and execute the function immediately.

if (window.cordova) {
  document.addEventListener("deviceready", onDeviceReady, false);
} else {
  onDeviceReady();
}

document.addEventListener("deviceready",onDeviceReady,false)

document.addEventListener("deviceready", onDeviceReady, false)

  • 参数1-"deviceready":事件的名称
  • 参数2-onDeviceReady:启动应用程序的函数的名称. 这个名字当然可以和我的名字不同.
  • 参数3-否 (可选):一个布尔值,它指定事件是否应该 在捕获阶段或冒泡阶段执行. (来源: w3schools.com)
  • Parameter 1 - "deviceready": The name of the event
  • Parameter 2 - onDeviceReady: The name of your function to starts your application. The name can of course differ to my name.
  • Parameter 3 - false (optional): A Boolean value that specifies whether the event should be executed in the capturing or in the bubbling phase. (Source: w3schools.com)

有关addEventListener的更多信息: https://www.w3schools.com/jsref/met_document_addeventlistener.asp

For more information about the addEventListener: https://www.w3schools.com/jsref/met_document_addeventlistener.asp

在"onDeviceReady"函数或更高版本中,您现在可以调用在config.xml中定义的插件

In your "onDeviceReady" function or later, you can now call the plugins you have defined in the config.xml

例如:

function onDeviceReady() {
  //call your plugins
  NativeStorage.getItem(<...>);
}

希望有帮助.

这篇关于为什么cordova-plugin-nativestorage在浏览器上起作用但在电话上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆