Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Knockout.js Typescript类继承:重写ko.computed方法_Knockout.js_Typescript - Fatal编程技术网

Knockout.js Typescript类继承:重写ko.computed方法

Knockout.js Typescript类继承:重写ko.computed方法,knockout.js,typescript,Knockout.js,Typescript,我有简单的课程: /// <reference path="..\typings\jquery\jquery.d.ts"/> /// <reference path="..\typings\knockout\knockout.d.ts"/> module Some.Namespace { export class TestBase { public field1: KnockoutObservable<string> = ko.ob

我有简单的课程:

/// <reference path="..\typings\jquery\jquery.d.ts"/>
/// <reference path="..\typings\knockout\knockout.d.ts"/>

module Some.Namespace {

    export class TestBase {
        public field1: KnockoutObservable<string> = ko.observable("");

        public onFieldChange: KnockoutComputed<string> = ko.computed(() => {
            return this.field1();
        }, this);
    }

    export class Test extends TestBase {
        public field2: KnockoutObservable<string> = ko.observable("");

        public onFieldChange() {
            super.onFieldChange() + this.field2();
        }
    }
}
//
/// 
模块名称空间{
导出类测试库{
公共字段1:KnockoutObservable=ko.observable(“”);
public onFieldChange:KnockoutComputed=ko.computed(()=>{
返回这个.field1();
},这个);
}
导出类测试扩展了TestBase{
公共字段2:KnockoutObservable=ko.observable(“”);
公共文件更改(){
super.onFieldChange()+this.field2();
}
}
}
问题,typescript不允许在重写方法中使用关键字super。它说:

错误1类“Some.Namespace.Test”无法扩展类 'Some.Namespace.TestBase':类'Some.Namespace.Test'定义 实例成员函数“onFieldChange”,但为扩展类 “Some.Namespace.TestBase”将其定义为实例成员属性

错误2只有基类的公共方法可以通过 “超级”关键字


如何重写敲除计算方法而不丢失基方法?

如果您自己定义了相同的名称,则无法从TypeScript中的子类访问父实例成员属性。e、 g.下列各项:

class TestBase {
    public field1;
    public onFieldChange = () => { // we need a way to reference this in the child
        return this.field1();
    };
}

class Test extends TestBase {

    parentOnFieldChange:any;
    constructor(){
        super(); // call super 

        // Implictly initialize local properties

        // copies local version not parent
        this.parentOnFieldChange = this.onFieldChange;
    }

    public field2;
    public onFieldChange = () => {
        this.parentOnFieldChange() + this.field2();
    }
}
生成(段):

解决方案使用实例成员函数:

class TestBase {
    public field1;

    public onFieldChange() {
        return this.baseOnFieldChange();
    }

    private baseOnFieldChange = () => { // we need a way to reference this in the child
        return this.field1();
    };
}

class Test extends TestBase {

    parentOnFieldChange:any;
    constructor(){
        super(); // call super              
    }

    public field2;
    public onFieldChange(){
        return super.onFieldChange() + this.childOnFieldChange();
    }

    private childOnFieldChange = () => {
        return this.field2();
    }
}

我有个主意。。。适应于高。计算。。。看起来有点乱。谢谢
class TestBase {
    public field1;

    public onFieldChange() {
        return this.baseOnFieldChange();
    }

    private baseOnFieldChange = () => { // we need a way to reference this in the child
        return this.field1();
    };
}

class Test extends TestBase {

    parentOnFieldChange:any;
    constructor(){
        super(); // call super              
    }

    public field2;
    public onFieldChange(){
        return super.onFieldChange() + this.childOnFieldChange();
    }

    private childOnFieldChange = () => {
        return this.field2();
    }
}