Ionic2/angularJs2中的靜態類 PhotoLibrary 調用不上,ionic2angularjs2

來源:互聯網
上載者:User

Ionic2/angularJs2中的靜態類 PhotoLibrary 調用不上,ionic2angularjs2
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();
}
 

--------------最終調用成功了-------------------

總結:如果遇到類似的靜態類,可以參照此方法。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.