提供的参数与调用目标的任何签名都不匹配 - Typescript

时间:2016-12-13 15:47:56

标签: angular typescript

我有一个名为FilterOperation.ts的模型,如下所示:

export class FilterOperations {
  constructor(
    public mainFilter : string,
    public currencyType : string,
    public status : string
    ) { }
}

然后让我的组件定义如下:

import { Component, OnInit, Input } from "@angular/core";
import { FormsModule }   from '@angular/forms';
import 'rxjs/operator/finally';

//models
import { Session }              from './src/models/session';
import { Client }              from './src/models/client';
import { Operation }              from './src/models/operation';
import { FilterOperations }              from './src/models/filterOperations';

//services
import { OperationSearchService }       from './src/services/operations_admin.service';

//constants
import * as constantsValues from "../../core/constants/constants";

@Component({
    templateUrl: './app/views/operations_admin/src/templates/operations_admin.html',
    styleUrls: ['./app/views/operations_admin/css/operations_admin.css'],
    providers: [ OperationSearchService ]
})

export class OperationsAdminComponent implements OnInit {
    @Input() filter: FilterOperations;
    errorMessage: string;
    clientFound: Client;
    sessionList: Session[];
    operationCount: number;
    mainSearchSelector: string;

    constructor (private operationSearchService: OperationSearchService) {
    }
    ngOnInit() {
        this.filter = new FilterOperations(); //HERE GOES THE ERROR
    }
    onChangeMainFilter(newValue) {
        this.mainSearchSelector = newValue;
    }
    findOperations() {
        //some code
    }
}

当我尝试编译它时,它会不断发送相同的消息:"提供的参数与呼叫目标的任何签名都不匹配"。初始化过滤器的值时我做错了什么:

this.filter = new FilterOperations();

1 个答案:

答案 0 :(得分:9)

FilterOperations的代码:

export class FilterOperations {
    constructor(
        public mainFilter : string,
        public currencyType : string,
        public status : string
    ) { }
}

相当于:

export class FilterOperations {
    public mainFilter: string;
    public currencyType: string;
    public status: string;

    constructor(mainFilter: string, currencyType: string, status: string) {
        this.mainFilter = mainFilter;
        this.currencyType = currencyType;
        this.status = status;
    }
}

虽然你可能会这样做:

export class FilterOperations {
    public mainFilter: string;
    public currencyType: string;
    public status: string;

    constructor() {}
}

在前两个构造函数中需要3个参数,但是当你实例化它时,你没有传递任何参数:

this.filter = new FilterOperations();

如果你想要一个空的ctor,那么请使用我的第三个代码片段 你也可以这样做:

export class FilterOperations {
    constructor(
        public mainFilter?: string,
        public currencyType?: string,
        public status?: string
    ) { }
}

既然所有参数都是可选的,你可以在没有参数的情况下调用ctor。