Я использую Angular 2 с TypeScript.Тип: Ссылка на несколько объектов разных типов, расширенных от одного базового типа
У меня есть BaseType
компоненту
@Component({})
export class BaseType {
protected state: string;
constructor() { }
setState(state : string) {
this.state = state;
}
}
Я простирающийся то же самое ComponentOne
и ComponentTwo
путем расширения выше BaseType
ComponentOne
@Component({})
export class ComponentOne extends BaseType {
constructor() { }
}
ComponentTwo
@Component({})
export class ComponentTwo extends BaseType {
constructor() { }
}
Я загрузке этих двух компонентов динамически в другом классе, используя следующий код (частичный код)
let component: any;
if(/*condition to show component one*/) {
this.component = ComponentOne;
} else {
this.component = ComponentTwo;
}
//Load Component
this
.loader
.loadIntoLocation(this.component, this.el, 'field')
.then((res) => {});
Когда я использую следующие позже в том же файле:
this.component.setState("state");
Неисправность запуска
TypeError: this.component.setState is not a function(…)
Какой тип я должен использовать на component
(в настоящее время это any
) или использовать любое литье, чтобы доступ к методу setState
на this.component
должен работать без проблем?
Заранее благодарен!