繁体   English   中英

如何在Angular Typescript中访问此关键字

[英]How can I access this keyword in Angular Typescript

我的角度项目中有一个打字稿类,如下所示:

export class CreateGeojsonLayerCommand implements Icommand {
    parameters: Object;

    execute(parameters: Object): Object {
        this.parameters = parameters;

        let layer = new Vector({            
            style:  createStyleFunction
        });
    }

    private createStyleFunction(feature: any): Style {
        //this.parameters is undefined here

    }
}

new Vector对象是第三方工具对象。 所以我已经从execute方法中创建了createStyleFunction 但是我无法使用this.parameters这样的关键字访问parameters变量。

您应该使用箭头功能,否则它指的是特定的上下文,

private createStyleFunction = (feature: any): Style => {

}

在使用createStyleFunction之前,请确保已execute函数,否则将不会实例化parameters 或向类添加构造函数以实例化parameters

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM