Ich möchte Extension-Methode zu nativen String-Typ hinzufügen.Ionic2 benutzerdefinierte Erweiterung hinzufügen Methode
String-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();
}
Verbrauch:
public slug: string = "UTF String".toSlug();
VS-Code-Vorschlag ohne Probleme funktioniert gut. Doch nach ionic serve
wirft es String.toSlug is not a function
Nach meiner CLI-Version ist:
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
Jede mögliche Hilfe geschätzt wird. Danke
können Sie den Code teilen, wo er importiert und verwendet wird? –
Ionic Build/Serve automatisch importieren Sie alle * .ts-Datei in src-Verzeichnis. Ich habe diese Zeile in 'tsconfig.js' gefunden:' "include": [ "src/**/*. Ts" ] ' – trinvh
http://stackoverflow.com/questions/39877156/how-to-e-tend- String-Prototyp-und-verwenden-es-nächste-in-typescript –