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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41125146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:04:42  来源:igfitidea点击:

Supplied parameters do not match any signature of call target - Typescript

angulartypescript

提问by Herbert Corazao

I have a model called FilterOperation.ts that goes like this:

我有一个名为 FilterOperation.ts 的模型,它是这样的:

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

Then have my component defined like this:

然后让我的组件像这样定义:

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
    }
}

It keeps sending the same message when I try to compile it: "Supplied parameters do not match any signature of call target ". What am I doing wrong when initializing the value of filter:

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

this.filter = new FilterOperations();

回答by Nitzan Tomer

Your code for FilterOperations:

您的代码FilterOperations

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

is equivalent to:

相当于:

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;
    }
}

Though you probably meant to do:

虽然你可能打算这样做:

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

    constructor() {}
}

In the first two the constructor expects 3 arguments, but when you instantiate it you're not passing any:

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

this.filter = new FilterOperations();

If you want an empty ctor then use my 3rd code snippet.
You can also do:

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

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

Now that all argument are optional you can call the ctor with no args.

现在所有参数都是可选的,您可以调用不带参数的 ctor。