我正在尝试使用离子2本机查询所有联系人。运行ionic serve命令时出现以下错误。如果有人知道请告诉我,我该如何解决这个问题。谢谢
Typescript Error
Property 'find' does not exist on type 'typeof Contacts'.
home.ts
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Contacts, Contact, ContactField, ContactName } from '@ionic-native/contacts';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public navCtrl: NavController) {
}
testFun(){
Contacts.find(['*']).then((contacts)=>{
alert(JSON.stringify(contacts[0]));
})
}
}
home.html的
<ion-header>
<ion-navbar>
<ion-title>
Ionic Blank
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<button ion-button full (click)="testFun()">Get Contacts</button>
</ion-content>
答案 0 :(得分:4)
Since Ionic 3.x.x, the way you use Native plugin is a little different from Ionic 2.x.x
Contact
in your constructorContact
in your @ComponentSo your home.ts should looks like this instead:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Contacts, Contact, ContactField, ContactName } from '@ionic-native/contacts';
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [Contacts]
})
export class HomePage {
constructor(public navCtrl: NavController, private contacts: Contacts) {
}
testFun(){
this.contacts.find(['*']).then((contacts)=>{
alert(JSON.stringify(contacts[0]));
})
}
}
More usage example check the official doc for Ionic 3.x NATIVE doc here http://ionicframework.com/docs/native/contacts/
答案 1 :(得分:0)
import { Component } from '@angular/core';
import { NavController, LoadingController, Platform } from 'ionic-angular';
import { SMS } from 'ionic-native';
import { Storage } from '@ionic/storage';
declare var navigator : any;
@Component({
selector: 'page-contact',
templateUrl: 'contact.html'
})
export class ContactPage {
isContactAccessConfirmed: boolean = false;
isFetching: boolean = false;
isInit: boolean = false;
contacts: Array<any> = [];
groupedContacts: Array<any> = [];
constructor(public navCtrl: NavController, public loadingCtrl: LoadingController, public platform: Platform, public storage: Storage) {
this.storage.get('isContactAccessConfirmed').then((val) => {
this.isContactAccessConfirmed = val;
this.isInit = true;
if (this.isContactAccessConfirmed) {
this.fetchContacts(null);
}
})
}
dismiss() {
this.navCtrl.pop();
}
confirm() {
let that = this;
this.fetchContacts(function() {
that.isContactAccessConfirmed = true;
that.storage.set('isContactAccessConfirmed', true);
});
}
invite(contact: any) {
if (contact.invited) {
return;
}
SMS.send(contact.number, 'Hey, come join me on my app! You can download it at: ...', {
android : {
intent: 'INTENT'
}
}).then(function() {
contact.invited = true;
}, function(error) {
console.log(error);
});
}
fetchContacts(cb) {
let loader = this.loadingCtrl.create({content: "Retrieving contacts..."});
this.platform.ready().then((readySource) => {
loader.present();
this.isFetching = true;
let that = this;
navigator.contactsPhoneNumbers.list(function(contacts) {
console.log(contacts.length + ' contacts found');
for(var i = 0; i < contacts.length; i++) {
console.log(contacts[i].id + " - " + contacts[i].displayName);
for(var j = 0; j < contacts[i].phoneNumbers.length; j++) {
var phone = contacts[i].phoneNumbers[j];
console.log("===> " + phone.type + " " + phone.number + " (" + phone.normalizedNumber+ ")");
that.contacts.push({
name: contacts[i].displayName,
number: phone.normalizedNumber,
invited: false
});
}
}
that.groupContacts(that.contacts);
that.isFetching = false;
loader.dismiss();
cb && cb();
}, function(error) {
console.error(error);
that.isFetching = false;
loader.dismiss();
});
});
}
searchContacts(ev: any) {
let val = ev.target.value;
if (val && val.trim() !== '') {
this.groupContacts(this.contacts.filter((contact) => {
return contact.name.toLowerCase().indexOf(val.toLowerCase()) > -1 ||
contact.number.toLowerCase().indexOf(val.toLowerCase()) > -1;
}));
}
else {
this.groupContacts(this.contacts);
}
}
groupContacts(contacts){
this.groupedContacts = []; // init grouped list
// sort all contacts by display name
let sortedContacts = contacts.sort(function(a,b) {
var x = a.name.toLowerCase();
var y = b.name.toLowerCase();
return x < y ? -1 : x > y ? 1 : 0;
});
let currentLetter = false;
let currentContacts = [];
// assign each contact to a group
sortedContacts.forEach((value, index) => {
if(value.name.charAt(0) != currentLetter){
currentLetter = value.name.charAt(0);
let newGroup = {
letter: currentLetter,
contacts: []
};
currentContacts = newGroup.contacts;
this.groupedContacts.push(newGroup);
}
currentContacts.push(value);
});
}
}
答案 2 :(得分:-1)
Contacts.prototype.find(['*'])
.then((contacts)=>{
alert(JSON.stringify(contacts[0]));
})
.catch((err) => {
alert('Error ' + err);
});