0
多角形を角度4で使用しようとしています。 しかし、子クラスのメンバーにアクセスしようとすると、 'ng build --prod'はエラー:/$$_gendir/app/app.component.ngfactory.ts(23,38):プロパティ 'bark'は '動物'型には存在しません。Angular 4のポリモーフィズムでエラーが発生する: 'childs field'の型が 'parent'型に存在しません。
これを修正する方法を知っている人はいますか?ありがとうございました。
app.component.html:
<div *ngIf="isDog(family)">
<!-- Property 'bark' does not exist on type 'Animal'. -->
<h4>{{family.bark}}</h4>
<!-- Parser Error: Missing expected) at column 9 -->
<h4>{{(family as Dog).bark}}</h4>
</div>
<div *ngIf="isCat(family)">
...
</div>
app.component.ts:
import {Component, OnInit} from '@angular/core';
export class Animal {}
export class Dog extends Animal {
bark: string;
}
export class Cat extends Animal {
meow: string;
}
const sampleData: Animal = { 'bark': 'wof'} as Dog;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'app';
family: Animal;
ngOnInit() {
this.family = sampleData;
}
}
しかし 'プロパティ「樹皮」Animal''あなたが期待する何をすべきか – yurzui
あなたが'試すことができファミリ」タイプに存在しません:犬; 'その後、typescriptですが、角コンパイル – yurzui
私の後に文句を言うべきではありません。?とにかく私は 'family:Animal;'を続けたいと思います。 '