Angularjs 爱奥尼亚2本机联系人插件查找不到';t出口

Angularjs 爱奥尼亚2本机联系人插件查找不到';t出口,angularjs,ionic2,ionic-native,Angularjs,Ionic2,Ionic Native,我正在尝试使用ionic 2 native查询所有联系人。运行ionic serve命令时出现以下错误。如果有人知道,我怎么解决这个问题请告诉我。多谢各位 Typescript错误 类型“typeof Contacts”上不存在属性“find”。 home.ts 从'@angular/core'导入{Component}; 从'ionic angular'导入{NavController}; 从'@ionic native/Contacts'导入{Contacts,Contacts,Conta

我正在尝试使用ionic 2 native查询所有联系人。运行ionic serve命令时出现以下错误。如果有人知道,我怎么解决这个问题请告诉我。多谢各位

Typescript错误
类型“typeof Contacts”上不存在属性“find”。
home.ts

从'@angular/core'导入{Component};
从'ionic angular'导入{NavController};
从'@ionic native/Contacts'导入{Contacts,Contacts,ContactField,ContactName};
@组成部分({
选择器:“主页”,
templateUrl:'home.html'
})
导出类主页{
构造函数(公共navCtrl:NavController){
}
testFun(){
联系人。查找(['*'))。然后((联系人)=>{
警报(JSON.stringify(联系人[0]);
})
}
}
home.html


离子空白
取得联系
错误图像


源代码

自从爱奥尼亚3.x.x以来,您使用本机插件的方式与爱奥尼亚2.x.x略有不同

  • 首先,您需要在构造函数中添加
    联系人
  • 您需要在@Component中添加提供者
    联系人
  • 因此,您的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',
      providers: [Contacts]
    })
    export class HomePage {
    
      constructor(public navCtrl: NavController, private contacts: Contacts) {
    
      }
    
      testFun(){
        this.contacts.find(['*']).then((contacts)=>{
          alert(JSON.stringify(contacts[0]));
        })
      }
    }
    
    更多使用示例请在此处查看官方文档,了解爱奥尼亚3.x原生文档

    您尝试过吗

    Contacts.prototype.find(['*'])
                .then((contacts)=>{
                    alert(JSON.stringify(contacts[0]));
                })
                .catch((err) => {
                    alert('Error ' + err);
    });
    
    从'@angular/core'导入{Component};
    从“离子角度”导入{NavController,LoadingController,Platform};
    从“离子本机”导入{SMS};
    从'@ionic/Storage'导入{Storage};
    声明var导航器:任何;
    @组成部分({
    选择器:“页面联系人”,
    templateUrl:'contact.html'
    })
    导出类联系人页面{
    isContactAccessConfirmed:boolean=false;
    isFetching:boolean=false;
    isInit:boolean=false;
    联系人:数组=[];
    groupedContacts:Array=[];
    构造函数(公共navCtrl:NavController,公共加载Ctrl:LoadingController,公共平台:平台,公共存储:存储){
    this.storage.get('isContactAccessConfirmed')。然后((val)=>{
    this.isContactAccessConfirm=val;
    this.isInit=true;
    如果(此.isContactAccessConfirm){
    this.fetchContacts(null);
    }
    })
    }
    解雇{
    this.navCtrl.pop();
    }
    确认{
    让那=这;
    this.fetchContacts(函数(){
    that.isContactAccessConfirm=true;
    that.storage.set('isContactAccessConfirm',true);
    });
    }
    邀请(联系人:任何){
    如果(请联系){
    返回;
    }
    SMS.send(contact.number,'嘿,来加入我的应用程序吧!你可以在以下位置下载:…'{
    安卓:{
    意图:“意图”
    }
    }).然后(函数(){
    contact.invested=true;
    },函数(错误){
    console.log(错误);
    });
    }
    获取联系人(cb){
    让loader=this.loadingCtrl.create({content:“检索联系人…”);
    this.platform.ready().then((readySource)=>{
    loader.present();
    this.isFetching=true;
    让那=这;
    navigator.contactsPhoneNumbers.list(功能(联系人){
    console.log(contacts.length+‘contacts found’);
    对于(变量i=0;i”+phone.type+“+phone.number+”(“+phone.normalizedNumber+”);
    那,推({
    姓名:联系人[i]。显示姓名,
    号码:phone.normalizedNumber,
    邀请:假
    });
    }
    }
    that.groupContacts(that.contacts);
    that.isFetching=false;
    loader.discouse();
    cb&&cb();
    },函数(错误){
    控制台错误(error);
    that.isFetching=false;
    loader.discouse();
    });
    });
    }
    搜索联系人(ev:任何){
    设val=ev.target.value;
    如果(val&&val.trim()!=''){
    this.groupContacts(this.contacts.filter)((contact)=>{
    return contact.name.toLowerCase().indexOf(val.toLowerCase())>-1 |
    contact.number.toLowerCase().indexOf(val.toLowerCase())>-1;
    }));
    }
    否则{
    this.groupContacts(this.contacts);
    }
    }
    组联系人(联系人){
    this.groupedContacts=[];//初始化分组列表
    //按显示名称对所有联系人进行排序
    让sortedContacts=contacts.sort(函数a,b){
    var x=a.name.toLowerCase();
    var y=b.name.toLowerCase();
    返回xy-1:0;
    });
    让currentLetter=false;
    设currentContacts=[];
    //将每个联系人分配给一个组
    sortedContacts.forEach((值,索引)=>{
    if(value.name.charAt(0)!=currentLetter){
    currentLetter=value.name.charAt(0);
    设newGroup={
    信函:当前信函,
    联系人:[]
    };
    currentContacts=newGroup.contacts;
    this.groupedContacts.push(newGroup);
    } 
    电流触点推压(值);
    });
    }
    }
    
    我尝试了你的解决方案,但没有成功,我还是遇到了同样的问题。请查看我的代码抱歉我以前写错了,我已经编辑了我的答案,使用
    this.contacts.find()
    而不是
    contact.find()
    我们还可以在app.module.ts文件而不是pages.ts文件中添加提供商。
    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);
        });
       }
    }