photoLibrary调用报错:No provider for PhotoLibrary;

在调用相册文件时有用到photolibrary,总有些莫名的报错,3月份的时候这个坑让我不知所措,现在写下来方便查看(也不知道Ionic2现在是否有变化),入坑过程:

按照官网示例:

1.添加插件:

cmd 到项目目录运行:

ionic plugin add cordova-plugin-photo-library --variable PHOTO_LIBRARY_USAGE_DESCRIPTION="To choose photos"

2. ts文件中添加:

import { PhotoLibrary } from '@ionic-native/photo-library';

constructor(private photoLibrary: PhotoLibrary) { }

  this.photoLibrary.requestAuthorization().then(() => {

    this.photoLibrary.getLibrary().subscribe({ next: library => {

    library.forEach(function(libraryItem) {

      console.log(libraryItem.id); // ID of the photo

       //........

  }); },

error: err => {}, complete: () => { console.log("could not get photos"); } }); })

.catch(err => console.log("permissions weren't granted"));

好,这一运行,就出现No provider for PhotoLibrary;

查了很久---------,终于发现了这个鬼;

在查看PhotoLibrary.java看到了一些静态的内部的类(没深入了解java---大概是这个意思):

public static XXX{}

而在java中的静态类 是不能使用继承的(使用 this、super关键字);

所以在.ts文件中就这样做-------->

import { PhotoLibrary } from '@ionic-native/photo-library';
constructor() { //1.构造函数中不注入PhotoLibrary
  
}
 theFunction(){
//2.直接使用--不用this
  PhotoLibrary.requestAuthorization().then(() => {     PhotoLibrary.getLibrary().subscribe({ next: library => {     library.forEach(function(libraryItem) {       console.log(libraryItem.id); // ID of the photo        //........   }); }, error: err => {}, complete: () => { console.log("could not get photos"); } }); }) .catch(err => console.log("permissions weren't granted"));
}
ionViewDidLoad(){
  //3.调用方法
  this.theFunction();
}
 

--------------最终调用成功了-------------------

总结:如果遇到类似的静态类,可以参照此方法。

最新文章

  1. Flymeos插桩适配教程
  2. //解决validator验证插件多个name相同只验证第一的问题
  3. C#将字节流加密解密
  4. ng-Enter指令
  5. The 10th Zhejiang Provincial Collegiate Programming Contest
  6. Timeout Detection & Recovery (TDR)
  7. [liu yanling]软件测试用例的基本要素包括哪些?
  8. webService 客户端调用及异常信息First Element must contain the local name, Envelope , but found definitions
  9. Java 读书笔记 (十七) Java 重写(Override)与重载(Overload)
  10. BBS论坛(十一)
  11. Facade 外观(结构型)
  12. C#简述(三)
  13. 使用 C++11 编写类似 QT 的信号槽——下篇
  14. ThinkPad T430i,如何将WIN8换成WIN7???
  15. Redis简介——(一)
  16. C#制作手机网站
  17. 20155216 2016-2017-2《Java程序设计》课程总结
  18. Spring框架第三篇之基于XML的DI注入
  19. jmeter属性设置
  20. shell脚本学习—Shell执行脚本

热门文章

  1. APPLE STORE
  2. HDOJ 4259 Double Dealing
  3. AHCI IDE
  4. node+express+mysql小例子
  5. javascript的继承方法
  6. Unity3D & C# 设计模式--23
  7. cocos2dx游戏开发学习笔记2-从helloworld開始
  8. 通过shell脚本批处理es数据
  9. input title 悬浮值
  10. struts2基础代码实现