【问题标题】:ionic 2 native contacts plugin find doesn't exit离子2本机联系人插件查找不退出
【发布时间】:2017-09-21 11:13:10
【问题描述】:

我正在尝试使用 ionic 2 native 查询所有联系人。运行 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>

错误图片

源码Git repo for my project

【问题讨论】:

    标签: angularjs ionic2 ionic-native


    【解决方案1】:

    从 Ionic 3.x.x 开始,使用 Native 插件的方式与 Ionic 2.x.x 略有不同

    1. 首先您需要在构造函数中添加Contact
    2. 您需要在您的@Component 中添加提供程序Contact

    所以你的 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]));
        })
      }
    }
    

    更多用法示例在此处查看 Ionic 3.x NATIVE 文档的官方文档http://ionicframework.com/docs/native/contacts/

    【讨论】:

    • 我尝试了您的解决方案,但没有运气,我仍然遇到同样的问题。
    • 对不起我之前写错了,我已经编辑了我的答案,使用this.contacts.find()而不是contact.find()
    • 我们也可以在 app.module.ts 文件而不是 pages .ts 文件中添加 Providers。
    【解决方案2】:

    你试过了吗?

    Contacts.prototype.find(['*'])
                .then((contacts)=>{
                    alert(JSON.stringify(contacts[0]));
                })
                .catch((err) => {
                    alert('Error ' + err);
    });
    

    【讨论】:

      【解决方案3】:
      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);
          });
         }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-05-08
        • 1970-01-01
        • 2018-04-10
        • 1970-01-01
        • 2018-03-27
        • 1970-01-01
        • 1970-01-01
        • 2018-01-24
        • 2017-09-26
        相关资源
        最近更新 更多