2017-10-07 44 views
1

当我处理不在基类和基类中的继承类型的成员时,Visual Studio代码(1.17.0)在Angular模板中生成错误在组件上声明。
在下面的代码中,QuestionBase上显然不存在maxLength,但我该如何处理?标识符'n'未定义,'object'不包含此类成员

[角度]标识符'maxLength'未定义。 'QuestionBase' 并不 没有包含这样的成员

export class QuestionBase { 
 
    id: number; 
 
    order: number; 
 
} 
 

 
export class TextQuestion extends QuestionBase { 
 
    maxLength: number 
 
}

在组件的问题被声明为QuestionBase,以便它可以成为任何类型的问题

@Import question: QuestionBase

现在,在HTML模板我ADDRES MaxLength属性:

<input type="text" class="form-control" id="maxLength" [(ngModel)]="question.maxLength" name="maxLength" /> 

回答

1

尝试这样的:

QuestionBase.ts

export class QuestionBase { 
    id: number; 
    order: number; 
} 

export class TextQuestion extends QuestionBase { 
    maxLength: number 
} 

component.ts

import { QuestionBase } from './'; 

export class Component { 

    private question: QuestionBase = new QuestionBase(); 

    constructor() { } 
} 

component.html

<input type="text" class="form-control" id="maxLength" [(ngModel)]="question.maxLength" name="maxLength" /> 
+0

Chandru您好,感谢。这工作(我的代码也执行和工作),但仍然intellisense给出提到的错误。 –

+0

您还没有声明基于QuestionBase Schema的问题对象,这是您所犯的错误。 – Chandru

0

我已经通过创建eacht questiontype一个单独的组件解决了这个问题。在每个控制器上,您可以在@Input()处指定questiontype。

<div [ngSwitch]="question.type"> 
    <app-text-question *ngSwitchCase="'text'" [question]="question"></app-text-question> 
    <app-checkbox-question *ngSwitchCase="'checkbox'" [question]="question"></app-checkbox-question> 
</div> 

TextQuestion

import { Component, OnInit, Input } from '@angular/core'; 
import { TextQuestion } from '../../_model/text-question'; 
@Component({ 
    selector: 'app-text-question', 
    templateUrl: './text-question.component.html', 
    styleUrls: ['./text-question.component.css'] 
}) 
export class TextQuestionComponent implements OnInit { 
@Input() question: TextQuestion ; 

constructor() { 
} 

ngOnInit() { 

而同为CheckboxQuestion:

import { Component, OnInit, Input } from '@angular/core'; 
import { CheckboxQuestion } from '../../_model/checkbox-question'; 

@Component({ 
    selector: 'app-checkbox-question', 
    templateUrl: './checkbox-question.component.html', 
    styleUrls: ['./checkbox-question.component.css'] 
}) 
export class CheckboxQuestionComponent implements OnInit { 
@Input() question: CheckboxQuestion 
    constructor() { } 

    ngOnInit() { 
    } 

}