2017-12-27 838 views
0

做有什麼問題沒有。在登錄到控制檯時顯示以下錯誤:角5類型錯誤:this.tipp.isPersistent不是一個函數

"Tipp: { 
"id":1, 
{...} 
} 

ERROR TypeError: this.tipp.isPersistent is not a function" is shown. 

第一個日誌語句顯示正確。但它似乎是一個問題,以評估 'this.tipp.isPersistent()':

@Component({ 
    selector: 'tipp-eingabe', 
    templateUrl: './tipp-eingabe.component.html', 
    styleUrls: ['./tipp-eingabe.component.css'], 
    encapsulation: ViewEncapsulation.None 
}) 
export class TippEingabeComponent implements OnChanges { 
    @Input() tipp: Tipp;  
    constructor() { 
    } 

    ngOnChanges(changes) { 
    console.log("Tipp: " + JSON.stringify(this.tipp)); 
    console.log("Tipp-isPersistent: " + this.tipp.isPersistent()); 
    } 
} 

export class Tipp { 
    id: number; 
    spieler: Spieler; 
    spiel: Spiel; 
    tippErgebnis: Spielstand; 
    aenderungsDatum: Date; 

    public isPersistent(): boolean { 
    return true; 
    }; 
} 

通過下面的模板片斷叫:

<div class="panel panel-default"> 
    <div class="panel panel-body"> 
    <div *ngFor="let spiel of spiele"> 
     <div *ngIf="!isMatchCollapsed(spiel.id)"> 
     <div *ngFor="let tipp of spiel.tipps" class="tippLine"> 
      <tipp-eingabe [tipp]="tipp"></tipp-eingabe> 
     </div> 
     </div> 
    </div> 
    </div> 
</div> 

回答

0

你應該只調用this.isPersistent()

console.log("Tipp-isPersistent: " + this.isPersistent()); 
+0

不起作用:編譯失敗:「財產‘isPersistent’上不存在型‘TippEingabeComponent’ – user1350889

2

它看起來像您正在創建與的屬性的對象而不是創建Tipp的新實例。這意味着您的對象具有Tipp的屬性,但不包括方法。

所以在通過TIPP到TippEingabeComponent你父組件,你應該創造Tipp一個新的實例。

let tipp = new Tipp(// pass in params); 

這意味着你需要更新你的蒂普類來接受參數

export class Tipp { 
    id: number; 
    spieler: Spieler; 
    spiel: Spiel; 
    tippErgebnis: Spielstand; 
    aenderungsDatum: Date; 

    constructor(obj: { 
     id: number; 
     spieler: Spieler; 
     spiel: Spiel; 
     tippErgebnis: Spielstand; 
     aenderungsDatum: Date; 
    }){ 
     this.id = obj.id; 
     this.spieler = obj.spieler; 
     this.spiel = obj.spiel; 
     this.tippErgebnis = obj.tippErgebnis; 
     this.aenderungsDatu = obj.aenderungsDatum; 
    } 

    public isPersistent(): boolean { 
     return true; 
    }; 
} 

現在,當你TippEngabeComponent調用該方法isPersistent它將在TIPP存在,因爲TIPP是蒂普類的一個實例。

+0

我怎樣才能做到這一點? 出口類高談闊論{ 。ID: 的TIPPS由一個服務從一個數據庫作爲高談闊論類的嵌入屬性遞送號碼; : tipps:Tipp []; } – user1350889

+0

@當你在你的父組件構建了'spiel.tipps'陣列user1350889,你應該創建一個類爲每個TIPP的新實例。所以像'spiel.tipps = [新蒂普(...),新蒂普(...),新蒂普(...)];' – LLai

0
ngOnChanges(changes) { 
    console.log("Tipp changed: " + JSON.stringify(this.tipp)); 
    console.log("this.tipp instanceof Tipp: " + (this.tipp instanceof Tipp)); 
    console.log("Tipp wurde geändert: " + this.tipp.isPersistent()); 
} 

再加些調試信息reults到:

Tipp changed: {"id":1, ..... } 
this.tipp instanceof Tipp: false 
ERROR TypeError: this.tipp.isPersistent is not a function 

由於日誌輸出稱,TIPP是不是蒂普類的一個實例。但爲什麼不呢? 數據作爲退出後端服務的複雜json樹的一部分提供。我認爲樹的嵌入式葉子/節點的實例化是通過角度來處理的?

export class Spiel { 
    id: number; 
    : 
    tipps: Tipp[]; 
} 

這是錯誤的假設,即嵌入式「的竅門」由數據服務交付JSON被實例化作爲「蒂普」類型爲「桌遊」類的定義聲明?

{ 
    "id": 1, 
    "tipps": [ 
    { 
     "id": 1, 
     "spieler": { 
     "id": 2, 
     "spielerName": "Stumbe", 
     "email": "[email protected]", 
     "rolle": "Spieler" 
     }, 
     "tippErgebnis": { 
     "toreTeam1": 1, 
     "toreTeam2": 2 
     }, 
     "aenderungsDatum": "2017-12-27T10:08:15" 
    }, 
    { 
     "id": 2, 
     "spieler": { 
     "id": 3, 
     "spielerName": "Aug", 
     "email": "[email protected]", 
     "rolle": "Admin" 
     }, 
     "tippErgebnis": { 
     "toreTeam1": 1, 
     "toreTeam2": 1 
     }, 
     "aenderungsDatum": "2017-12-27T10:08:15" 
    } 
    ] 
} 
+0

JSON從數據服務返回是簡單地JSON。角度http客戶端負責檢索您的數據。您有責任根據需要轉換該數據。在你的情況下,實例化你的自定義類。做'tipps:Tipp []'只意味着'tipps'是'Tipp'的數組類型。它不做任何實例化。 – LLai

+0

這似乎不是一個答案? – Alex

相關問題