2017-07-10 38 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

得到它谢谢! –