我在我的课程中使用箭头功能而不是方法,因为它们允许我使用"这个"关键字,并保持"这个"意思是类本身,而不是方法。
我有一个抽象类,它定义了一个由子类重写的空箭头函数。更正,原来是这样的:
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 = () => {};
但是当我试图覆盖它时,它只是不会覆盖它,所以子类中的_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);
}
}
};
}
}
如何成功覆盖子类中的_configureMultiselect
?
答案 0 :(得分:1)
如何成功覆盖子类中的_configureMultiselect
将其存储在superConfigureMultiselect
的新成员中,然后再使用。
答案 1 :(得分:0)
我仍然不知道如何在父级中使用空箭头功能并在子级中覆盖它。
对于我的场景,我可以完全删除该功能 - 这是多余的。我只需要覆盖multiselectConfiguration: Object;
属性。