Inheritance 重写子类中的箭头函数
我在类中使用箭头函数而不是方法,因为它们允许我使用“this”关键字,并保留“this”表示类本身,而不是方法 我有一个抽象类,它定义了一个空的arrow函数,该函数将被子类重写。更正,原来是这样的:Inheritance 重写子类中的箭头函数,inheritance,typescript,arrow-functions,Inheritance,Typescript,Arrow Functions,我在类中使用箭头函数而不是方法,因为它们允许我使用“this”关键字,并保留“this”表示类本身,而不是方法 我有一个抽象类,它定义了一个空的arrow函数,该函数将被子类重写。更正,原来是这样的: abstract _configureMultiselect(): void; 但试图用箭头函数覆盖它会导致: Class 'CategorySelect' defines instance member function '_configureMultiselect', but extende
abstract _configureMultiselect(): void;
但试图用箭头函数覆盖它会导致:
Class 'CategorySelect' defines instance member function '_configureMultiselect', but extended class 'SearchFilterCategorySelect' defines it as instance member property.
(property) Select.SearchFilterCategorySelect._configureMultiselect: () => void
因此,我在父类中将其更改为空箭头函数:
_configureMultiselect = () => {};
但是当我试图重写它时,它只是不重写它,所以我的子类中的\u configureMultiselect
是空的,而我显然希望它有一个函数。这是我的密码:
interface ICategorySelect {
multiselectConfiguration: Object;
numberOfCategories: KnockoutObservable<number>;
allSelected: KnockoutObservable<boolean>;
selectedCategories: KnockoutObservableArray<Category>;
categories: KnockoutObservableArray<Category>;
}
abstract class CategorySelect implements ICategorySelect {
multiselectConfiguration: Object;
numberOfCategories: KnockoutObservable<number>;
allSelected: KnockoutObservable<boolean>;
selectedCategories: KnockoutObservableArray<Category>;
categories: KnockoutObservableArray<Category>;
constructor() {
this._configureMultiselect();
this._instantiateCategories();
this.numberOfCategories = ko.observable(7);
this.allSelected = ko.observable(false);
}
_configureMultiselect = () => {};
_instantiateCategories = () => {
this.categories = ko.observableArray([
new Category("Meat", 1),
new Category("Dairy", 2),
new Category("Confectionary", 3),
new Category("Dessert", 4),
new Category("Baking", 7),
new Category("Grocers", 6),
new Category("Restaurants", 5),
new Category("Condiments", 8),
new Category("beverages", 9),
]);
}
}
export class SearchFilterCategorySelect extends CategorySelect {
constructor() {
super();
}
_configureMultiselect = () => {
this.multiselectConfiguration = {
buttonWidth: '100%',
buttonContainer: '<div style="height: 64px;" />',
buttonClass: 'none',
nonSelectedText: "select categories",
nSelectedText: ' thingymajigs',
allSelectedText: "all of the things!!",
selectAllNumber: false,
includeSelectAllOption: true,
disableIfEmpty: true,
onSelectAll: () => {
this.allSelected(true);
},
onInitialized: () => {
},
onChange: (option, checked) => {
this.selectedCategories = ko.observableArray([]);
var self = this;
$('#category-select option:selected').each(function() {
self.selectedCategories.push(
<Category>($(this).text(), $(this).val())
)
});
console.log(this.selectedCategories());
if(this.selectedCategories().length < this.numberOfCategories()) {
this.allSelected(false);
}
}
};
}
}
interface-ICategorySelect{
多选配置:对象;
numberOfCategories:可观察到的敲除;
所有选择:敲除可观察;
所选类别:击倒观察到的耳环;
类别:淘汰赛;
}
抽象类CategorySelect实现了ICategorySelect{
多选配置:对象;
numberOfCategories:可观察到的敲除;
所有选择:敲除可观察;
所选类别:击倒观察到的耳环;
类别:淘汰赛;
构造函数(){
这是。_配置MultiSelect();
这个。_实例化范畴();
此.numberOfCategories=ko.可观察(7);
this.allSelected=ko.可观察(假);
}
_configureMultiselect=()=>{};
_实例化类别=()=>{
this.categories=ko.array([
新类别(“肉类”,1),
新类别(“乳制品”,2),
新类别(“糖果”,3),
新类别(“甜点”,4),
新类别(“烘焙”,7),
新类别(“杂货商”,6),
新类别(“餐厅”,5),
新类别(“调味品”,8),
新类别(“饮料”,9),
]);
}
}
导出类SearchFilterCategorySelect扩展了CategorySelect{
构造函数(){
超级();
}
_configureMultiselect=()=>{
此.multiselectConfiguration={
按钮宽度:“100%”,
按钮容器:“”,
按钮类:“无”,
非选定文本:“选择类别”,
选择文本:“thingymajigs”,
allSelectedText:“所有的东西!!”,
selectAllNumber:false,
includeAlloption:true,
disableIfEmpty:true,
onSelectAll:()=>{
此选项为.allSelected(true);
},
初始化:()=>{
},
onChange:(选项,选中)=>{
this.selectedCategories=ko.observearray([]);
var self=这个;
$(“#类别选择选项:选定”)。每个(函数(){
self.selectedCategories.push(
($(this.text(),$(this.val())
)
});
console.log(this.selectedCategories());
if(this.selectedCategories().length
如何在子类中成功重写\u配置MultiSelect
如何在子类中成功重写_configureMultiselect
将其存储在新成员中,作为超级配置MultiSelect
,然后稍后使用
更多
这里介绍了Arrow函数继承:我仍然不知道如何在父函数中包含空Arrow函数并在子函数中重写它
对于我的场景,我可以完全删除该函数——这是多余的。我只需要覆盖
multiselectConfiguration:Object代码>属性。无法使其工作。你能为我的代码做一个代码示例吗?我的代码与你答案中的链接不同,因此它并不适用。在链接中,父方法有一个实现