超负荷工作Angular 2

时间:2017-04-11 08:43:28

标签: angular typescript

所以我尝试重载此方法,但TypeScript一直在调用它的重复方法。显然是什么,但我想超载它。

    import {Injectable, EventEmitter} from '@angular/core';

@Injectable()
export class NotificationService extends EventEmitter<any>{
    /**
     *
     */
    constructor() {
        super(false);

    }

    public error(message:string ):void{
        this.emit({message: message, type: 'error'});       
    }

    public error(message:string, type:string):void {
        this.emit({message: message, type: type});
    }

    public success(message:string):void{
        this.emit({message: message, type: 'success'})
    }


}

我尝试了一些其他的东西,它仍然给出了相同的错误。

  import {Injectable, EventEmitter} from '@angular/core';

@Injectable()
export class NotificationService extends EventEmitter<any>{
    /**
     *
     */
    constructor() {
        super(false);

    }

    public error(message:string, type?:string ):void{
        this.emit({message: message, type: 'error'});       
    }

    public error(message:string, type:string):void {
        this.emit({message: message, type: type});
    }

    public success(message:string):void{
        this.emit({message: message, type: 'success'})
    }


}

它需要发出一个http api响应代码。

3 个答案:

答案 0 :(得分:1)

Typescript和javascript世界一般不支持重载。 要实现您的目标,您必须创建一个处理可选参数的错误方法。

ecma6和typescript添加了class关键字,但javascript范例没有改变。 Javascript基于原型,它基本上是一个大键值数组。如果您创建两个具有相同名称的方法,则第二个方法将覆盖第一个方法。

答案 1 :(得分:1)

所以我现在所做的很可能正在工作

import {Injectable, EventEmitter} from '@angular/core';

@Injectable()
export class NotificationService extends EventEmitter<any>{
    /**
     *
     */
    constructor() {
        super(false);

    }

    public error(message:string, type?:string):void {
        this.emit({message: message, type: type?type:'error'});
    }

    public success(message:string):void{
        this.emit({message: message, type: 'success'})
    }


}

答案 2 :(得分:1)

javascript中没有重载,打字稿是javascript超集,你也不会因打字稿而过载。使用Javascript / typescript对象的方法由名称标识。

实际上,由于您可以定义可选参数和默认值,因此使用typescript进行重载是没用的。写这个:

public error(message:string, type?:string):void {
    this.emit({message: message, type: type?type:'error'});
}

将等同于重载error方法(如果允许重载),一个定义带有type参数,另一个带有foo?foo:bar参数。

另请注意,您可以为type设置默认值,而不是仅使其成为可选项,而不是使用三元运算符({foo:foo})。 最后一点,如果定义值的变量与属性具有相同的名称({foo}在typescript和es6中足够了),则不必在对象中写public error(message:string, type:string="error"):void { this.emit({message, type}); }

LIST.delete(0,END) # this is a list view
usePATH = '/nfzcae/nvh_mdp/Messdatenbank_Powertrain'
fileLevels = [] # code of interest is below

for path, dirs, f in os.walk(usePATH):
    for d in dirs:
        for f in glob.iglob(os.path.join(path, d,'*COMPARE.mat')):
            if 'COMPARE.mat' in f:                                          # if 'COMPARE.mat' in f and not 'MIN' in f and not 'MAX' in f / if 'COMPARE.mat' in f )   # if 'COMPARE.mat' in f and not 'MIN' in f and not 'MAX' in f
                fileLevels.append(f.split('/'))                             # Split path string at all '/'
                LIST.insert(END,f)                                          # Insert path with name string into the listbox
                LIST.update_idletasks()                                     # Update listbox after each element added
                nr = LIST.size()                                            # Get current number of files in listbox
                VAR.set(nr)                                                 # Set number of files as a variable for label
                LIST.see(END)                                               # See last element added
                FILE_LIST.append(f)
            else:
                pass                                                        # Do nothing
LIST.insert(END,'Search finished')                                          # Marks conclusion of search process