2016-12-10 3 views
3

기본 String 유형에 확장 메소드를 추가하려고합니다.Ionic2 사용자 정의 확장 메소드 추가

문자열 extension.ts :

사용
interface String { 
    toSlug(): string; 
} 

String.prototype.toSlug = function() { 
    var str = this.toLowerCase(); 
    str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ |ặ|ẳ|ẵ/g, "a"); 
    str = str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g, "e"); 
    str = str.replace(/ì|í|ị|ỉ|ĩ/g, "i"); 
    str = str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ |ợ|ở|ỡ/g, "o"); 
    str = str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g, "u"); 
    str = str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g, "y"); 
    str = str.replace(/đ/g, "d"); 
    str = str.replace(/!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'| |\"|\&|\#|\[|\]|~|$|_/g, " "); 
    str = str.replace(/-+-/g, " "); 
    str = str.replace(/^\-+|\-+$/g, ""); 
    return str.trim(); 
} 

:

public slug: string = "UTF String".toSlug(); 

VS 코드 제안은 아무 문제없이 잘 작동합니다. 그러나 ionic serve 후 내 CLI 버전입니다 다음 String.toSlug is not a function

를 던졌습니다 :

Cordova CLI: 6.4.0 Ionic Framework Version: 2.0.0-rc.3 Ionic CLI Version: 2.1.13 Ionic App Lib Version: 2.1.7 Ionic App Scripts Version: 0.0.45 ios-deploy version: Not installed ios-sim version: Not installed OS: Windows 10 Node Version: v6.9.1

어떤 도움을 이해할 수있을 것이다. 감사합니다.

+0

가져오고 사용하는 코드를 공유 할 수 있습니까? –

+0

이오니아 빌드/src 디렉토리의 * .ts 파일 자동 가져 오기. 이 줄은'tsconfig.js'에 있습니다 :''include ': [ "src/**/*. ts" ]' – trinvh

+0

http://stackoverflow.com/questions/39877156/how-to-extend- string-prototype-and-it-next-in-typescript –

답변

1

답변을 찾았습니다. 솔루션 app.module.ts에 오기 확장 파일입니다 :이 (2 + 각도)에

import '../extentions/string.ts'; 
import '../extensions/array.ts'; 
... 

변경 프로토 타입 :

String.prototype.toSlug = function (this:string) { ... } 

문제는 컴파일하는 동안 이온 앱 스크립트 모듈 파일을 포함하지 않는 것입니다.