Static PhotoLibrary in Ionic2/angularJs2 cannot be called, ionic2angularjs2

Source: Internet
Author: User

Static PhotoLibrary in Ionic2/angularJs2 cannot be called, ionic2angularjs2
PhotoLibrary call error: No provider for PhotoLibrary;

Photolibrary is used when calling photo album files. There are always some inexplicable errors. I was overwhelmed by this pitfall in March. Now I can write it down to make it easy to check (I don't know if Ionic2 is changing now ), pitfall process:

Follow the official website example:

1. Add a plug-in:

Run cmd in the project directory:

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


2. Add the following to the ts file:

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"));

Okay, this operation will show No provider for PhotoLibrary;

I checked it for a long time --------- and finally found this ghost;

In PhotoLibrary. java, I saw some static internal classes (I didn't know much about java-this is probably the meaning ):

public static XXX{}

Static classes in java cannot use inheritance (using the this and super keywords );

So do this in the. ts file -------->

import { PhotoLibrary } from '@ionic-native/photo-library';
Constructor () {// 1. No PhotoLibrary is injected into the constructor.
  
}
TheFunction () {// 2. use it directly -- this PhotoLibrary is not required. requestAuthorization (). then () => {PhotoLibrary. getLibrary (). subscribe ({next: library =>{ library. forEach (function (libraryItem) {console. log (libraryItem. id); // ID of the photo //........});}, error: err =>{}, complete: () =>{ console. log ("cocould not get photos ");}});}). catch (err => console. log ("permissions weren't granted "));}
IonViewDidLoad (){
// 3. Call the Method
This. theFunction ();
}
 

-------------- The call is successful -------------------

Conclusion: If you encounter a similar static class, you can refer to this method.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.