2017-07-10 35 views

回答

2

首先,你必須安裝libphonenumber-js,這是谷歌,libphonenumber的準備對角2+進口的包裝。 您可以在您的應用程序安裝:根據你所使用的包管理器

npm install libphonenumber-js --save 

yarn add libphonenumber-js 

安裝後,您可以使用它您的組件像:

import { Component, OnInit } from '@angular/core'; 
import { parse, format, asYouType } from 'libphonenumber-js'; 


@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 
export class AppComponent implements OnInit { 

    asYouType: any; 
    format: any; 
    parse: any; 

    ngOnInit() { 
    this.asYouType = new asYouType('US').input('2133734'); 
    this.format = format('2133734253', 'US', 'International'); 
    this.parse = parse('(0777) 844 822', 'RO'); 
    } 

} 

我加在Github上的工作演示:

libphonenumber-demo-angular2

+0

我安裝使用NPM,你上面提到的,當我進口在「libphonenumber-JS」我得到的錯誤是不能優良的模塊「libphonenumber-JS」。 –

+0

得到它謝謝! –