首页 > 解决方案 > Typescript Lambda 属性不是函数

问题描述

我是打字稿的新手,我正在尝试使用别人的插件我已经向他们寻求帮助,但我不确定需要多长时间。该仓库一年多没有更新。

我正在尝试创建一个定义为的 NotyButton:

interface NotyButton {
new(text: string, 
    classNames: string, 
    cb: Function, 
    attributes: any): NotyButton
}

NotyButton 在 NotyOptions 中使用:

export interface NotyOptions {
type?: NotyType;
layout?: NotyLayout;
theme?: NotyTheme;
text?: string;
timeout?: false | number;
progressBar?: boolean;
closeWith?: ('click' | 'button')[];
animation?: {
    open?: string | null | Function,
    close?: string | null | Function
};
id?: false | string;
force?: boolean;
killer?: boolean | string;
queue?: string;
container?: false | string;
buttons?: NotyButton[],
callbacks?: {
    beforeShow?: () => void,
    onShow?: () => void,
    afterShow?: () => void,
    onClose?: () => void,
    afterClose?: () => void,
    onHover?: () => void,
    onTemplate?: () => void
};
sounds?: {
    sources?: string[],
    volume?: number,
    conditions?: string[]
};
docTitle?: {
    conditions?: string[]
};
modal?: boolean,
}

NotyModel 类中有一个静态方法,它公开了一个使用 lambda 表达式返回 NotyButton 的按钮属性(如果我没看错的话)。我相信这就是我的问题所在。

class NotyModel {
    static button: (text: string, classNames: string, cb: Function, attributes?: any) => NotyButton;
}

我必须在 NgNoty 类的 create 方法中使用 NotyOptions 对象作为参数:

declare let Noty: any;

@Injectable()
export class NgNoty {

create(options: NotyOptions): NotyModel {
    return new Noty(options);
}

closeAll(name?: string) {
    Noty.closeAll(name);
}

setMaxVisible(count: number, name?: string) {
    Noty.setMaxVisible(count, name);
}
}

我有一个组件创建,它试图像这样使用 NgNoty 的创建方法:

    this.noty.create(<NotyOptions>{
    text: 'Problems Encountered',
    layout: 'bottomRight',
    theme: 'bootstrap-v4',
    type: 'error',
    buttons: [ 
      NotyModel.button('Ok', 'btn', () => {}, null)
    ]
}).show();

当我使用 NotyModel.button 时,我得到NotyModel.button is not a function

标签: typescript

解决方案


看来 ng-noty 的作者可能忘记实现NotyModel.button. 我试图在这个 fork中实现它,但我不熟悉如何测试它。试试看,如果它适合你,我们中的一个可以提交一个拉取请求。(请参阅此答案以了解我将 npm 包的本地修改版本添加到您的应用程序的首选技术。)


推荐阅读