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.