Ionic 2:使用Cordova插件

Ionic 2:使用Cordova插件

问题描述:

我做了很多谷歌搜索,似乎无法得到很多答案,在Ionic 2中调用Cordova插件的语法是如何工作的。

I did a lot of googling and couldn't seem to come up with much of an answer, how does the syntax of calling Cordova plugins in Ionic 2 work.

例如,在Ionic 1:
我使用的是facebook插件,我会称之为:$ cordovaFacebook.login([public_profile ,email,name,last_name,first_name,birthday,age_range,link])。then(function(success){

Like for example, in Ionic 1: I was using a facebook plugin, and I would call it like: $cordovaFacebook.login(["public_profile", "email","name","last_name","first_name","birthday","age_range","link"]).then(function (success) {

我也会注入它并将其作为依赖项添加。这两件事我不知道如何处理Ionic 2。

I would also 'inject' it and add it as a dependency. Both things I am not sure how to do with Ionic 2.

我在某处阅读了离子2不是必须的,但我仍然不确定

I read somewhere that with Ionic 2 that isn't as nesscary, but I am still unsure

使用cordova插件的步骤:

Steps to use cordova plugin:


  1. 添加插件

  1. Add plugin

cordova插件添加[插件名称]

cordova plugin add [name of plugin]

实际上这是使用cordova插件的唯一步骤。但是当你使用Typescript时可能会出现错误信息。例如:

Actually this is the only step to use cordova plugin. But there could be an error message when you're using Typescript. For example:


[默认] /Users/myname/Projects/ionic2/demo/app/pages/home/home.ts:21中的错误: 12
物业'设备'不存在o n键入'Navigator'。

ERROR in [default] /Users/myname/Projects/ionic2/demo/app/pages/home/home.ts:21:12 Property 'device' does not exist on type 'Navigator'.




  1. 修复错误消息打字稿。您可以简单地将此行添加到.ts文件中,例如:

  1. To fix the error message for Typescript. You can simple add this line to your .ts file, for example:

声明var navigator:any;

declare var navigator : any;