将草稿椅与Typescript一起使用 [英] Using lawnchair with Typescript

查看:106
本文介绍了将草稿椅与Typescript一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决了一个简单的问题,该问题在打字稿的"this"范围内也有类似的问题.

Got a simple problem, which was touched upon in a similar question around the "this" scope with typescript.

我有一个存放服务,可以将草椅包裹起来以解决我当地的存放问题.现在,我将这段代码迁移到Typescript上,遇到了一个我无法解决的问题.

I have a storage service which wraps lawnchair for my local storage concerns. Now I am migrating this code over to Typescript I have come to an issue I cannot solve.

public GetAll(callback: Function) {
           Lawnchair({ name: this.storeName }, function () {
                this.all(callback);
            });
        };

现在在上面的草席示例中,使 this 成为您要操作的实际商店,但是Typescript使其成为您正在使用的类的实例.那么有没有办法解决这个问题?

Now in the example above lawnchair makes this become the actual store you are actioning, but Typescript will make it become the instance of the class you are using. So is there a way around this issue?

==编辑==

略微更新了问题示例,以免与基础草椅方法和周围环境类方法混淆.

Updated the question example slightly as to not confuse with the underlying lawnchair method and the surrounding classes method.

这是我尝试编译时遇到的错误:

Here is the error I get when trying to compile:

类型的值不存在属性"all" "LawnchairStorageService"

The property 'all' does not exist on value of type 'LawnchairStorageService'

这是正确的,该方法在包含的类实例上不存在,它存在于草席闭合范围 this 的上下文中.

Which is correct, that method does NOT exist on the containing class instance, it exists within the context of the lawnchair closure scopes this.

推荐答案

如上面的注释中所述,它可以编译,并且我认为它应该运行:

As noted in the comments above, this compiles, and I think it ought to run:

public GetAll(callback: Function) {
    return function (callback){
        Lawnchair({ name: this.storeName }, function () {
            this.all(callback);
        });
    }(callback);
};

...但是我怀疑其他人也许可以用更优雅的解决方案来权衡.

... but I suspect someone else may be able to weigh in with a more elegant solution.

这篇关于将草稿椅与Typescript一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆