Angular 4/2中的方法重载

前端之家收集整理的这篇文章主要介绍了Angular 4/2中的方法重载前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我现在正在使用Angular 4.我没有在Angular 2或4中找到任何关于方法重载的正确解决方案.是否有可能在角度服务类中实现方法重载?或者我有兴趣知道它的细节.提前致谢.

我刚刚尝试像下面那样创建服务但发现Dublicate函数实现错误

ApiService.ts:

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

@Injectable()
export class ApiService {

       constructor() { }

       postCall(url,data,token) { // with three parameters
                 return resultFromServer; }        

       postCall(url,data) { // with two parameters
                return resultFromServer;}          
       }

AuthenticationService.ts:

import { Injectable } from '@angular/core';
import { ApiService } from "app/_services/api.service";
import FunUtils from "app/_helper/utils";

@Injectable()
export class AuthenticationService {

    constructor(private api: ApiService) { }

    rest_login_call(userName,password) {
        let data = { username: userName,password: password };
        let url = "http://localhost:8000";
        return this.api.postCall(url,data);
    }

}

解决方法

而不是重载方法,使令牌参数可选.

postCall(url,token?) { // with three parameters
             return resultFromServer; 
}

希望能帮助到你

猜你在找的Angularjs相关文章