2017-08-01 34 views
0

我在我的Angular應用程序中擴展typescript字符串接口。我已經添加了一個可以在我的應用程序中訪問的方法translate()。 我沒有收到任何編譯錯誤。Typescript擴展字符串接口運行時錯誤

不過,我得到一個運行時錯誤:

"TypeError: "Translate this string".translate is not a function"

任何想法我可能是做錯了?


這裏是我執行的截圖:

聲明:

enter image description here

實施

enter image description here

呼叫:

enter image description here

+0

我沒有看到任何[延伸](https://www.typescriptlang.org/docs/handbook/classes.html)在這裏,所謂的只是一個接口'String'。 – crashmstr

+0

@crashmstr我不需要這裏的擴展,因爲我在現有的typescript字符串接口中實現了一個新的方法,並將在全局範圍內使用它。 – Faisal

回答

1

我能解決這個問題。下面是關於如何解決此問題的更新:


1. Create a file global.d.ts and add the interface extension definitions there.

export { }; 
declare global 
{ 
    interface String 
    { 
     /** 
     * Translates the given string according to the culture provided. 
     * @param cultureName The culture name of the translation target. 
     * @returns The translated string. 
     */ 
     translate(cultureName: string): string; 
    } 
} 

2. Create another file with the interface extension methods definition. In my case I named it string.extensions.ts

/** 
* Translates the given string according to the culture provided. 
* @param cultureName The culture name of the translation target. 
* @returns The translated string. 
*/  
String.prototype.translate = function(cultureName: string): string 
{ 
    const inputValue = this; 

    // Do the translation here 
    const translatedValue = 'Willkommen bei meiner App'; // Only for example 

    return translatedValue ; 
}; 

3. In your app boot file, in my case its main.ts , add a reference to the global.d.ts and the file containing your extension methods definitions.

/// <reference path="app/core/extensions/global.d.ts" /> 
//... 
import './app/core/extensions/string.extensions'; 

你只需要在你的項目一旦導入此文件(main.ts)然後你可以在代碼中的任何地方使用它。

4. Example use in my AppComponent

import {Component} from '@angular/core';  

@Component({ 
    selector: 'my-app', 
    template: ` 
     Original Value: <h3>{{ originalValue }}</h3> 
     Translated Value: <h3>{{ translatedValue }}</h3> 
    ` 
}) 
export class AppComponent { 

    private originalValue:string; 
    private translatedValue:string; 

    constructor() {   
     this.originalValue = 'Welcome to my App'; 
     this.translatedValue = 'Welcome to my App'.translate('de-DE'); 
    }  
} 

這就是所有你需要做的來解決這個惱人的問題。

這裏是工作plunker鏈接:Plunker Demo

1

這個工作對我來說:

聲明接口

interface String { 
    translate():string 
} 

使用

String.prototype.translate = function(){ 
    return "boink" 
} 

let str : string = "test"; 
let a : string = str.translate(); 
console.log(a); // logs "boink" 
+0

當我在單獨的文件中聲明時,這不起作用。 – Faisal

+0

這裏是我的朋友的鏈接:https://plnkr.co/edit/bn9iSjvXyvotRps38mYw – Faisal

+0

你是對的,它似乎不適用於Angular。當我在非Angular項目中嘗試完全相同的代碼時,它的工作原理! – Kokodoko

1

我猜你的聲明是在一個單獨的文件中。它仍然使用基本聲明。它無法訪問代碼。嘗試使用文件或此文件的直接導入。

///<reference path="test.d.ts"/> 
+0

是的,我的聲明在一個單獨的文件中。我已經包含了參考路徑,但沒有幫助。 – Faisal

+0

這裏是我的朋友的鏈接:https://plnkr.co/edit/bn9iSjvXyvotRps38mYw – Faisal

+1

爲什麼你不使用管道來處理這個商業案例? |翻譯? –