Typescript 编译错误;应为1个参数,但得到0个

Typescript 编译错误;应为1个参数,但得到0个,typescript,angular-cli,Typescript,Angular Cli,这是我的第一个angular项目,我一直在使用内置服务器,偶尔也会做一个构建来推送到服务器上——现在我正在尝试一个--prod构建……我遇到了编译错误 经过一段时间的阅读,我已经解决了其中的大部分问题,并且我理解AOT的需求,但是这个错误让我感到困惑 我通过应用程序反复使用此代码来启动表单 <form novalidate="novalidate" autocomplete="off" [formGroup]="form" (submit)="form.valid

这是我的第一个angular项目,我一直在使用内置服务器,偶尔也会做一个构建来推送到服务器上——现在我正在尝试一个--prod构建……我遇到了编译错误

经过一段时间的阅读,我已经解决了其中的大部分问题,并且我理解AOT的需求,但是这个错误让我感到困惑

我通过应用程序反复使用此代码来启动表单

<form novalidate="novalidate"
    autocomplete="off"
    [formGroup]="form"
    (submit)="form.valid && submitForm()" >
其中第6行是:(submit)=“form.valid&&submitForm()”>

================== 注意:我已经将代码简化为一个简单的框架

and included it here, so you see EXACTLY what is there...

no extrapolating or generalizing.

Builds fine in dev mode
cli v(1.6.0) 我仍然得到相同的错误-user form.component.html(6,4):预期参数为1,但得到0

所以我想我应该给出完整的组件和模板

组件。ts

import { Component, OnInit, EventEmitter, Output } from '@angular/core';
import { FormBuilder, FormGroup, FormControl, NgForm, NgControl, Validators } from '@angular/forms';
import { AbstractControl } from '@angular/forms/src/model';
import { ValidationErrors } from '@angular/forms/src/directives/validators';

import { get } from 'lodash';

@Component({
    selector: 'user-form',
    templateUrl: './user-form.component.html',
})

export class UserFormComponent implements OnInit {

    form: FormGroup;
    loading = false;
    submitAttempted = false;

    constructor(
    ) { }

    get formDisabled () {
        return this.loading === true;
    }

    get xStatus () {
        const field = this.form.get('x');
        if (!field.touched) {
            return '';
        }
        if (field.errors && field.errors.required) {
            return 'This field is required';
        }
    }


    get formModel () {
        return {
            x: this.form.get('x').value
        }
    }

    ngOnInit() {
        this.form = new FormGroup({
            x: new FormControl(
                {
                    value: 'x',
                    disabled: this.formDisabled,
                },
                [
                    Validators.required
                ]
            )
        })
    }

    resetForm () {
        this.form.get('x').markAsUntouched();
        this.submitAttempted = false;
    }

    submitForm(form: NgForm) {
        console.log( this.form );
    }

    showError (fieldName: string) {
        const field = this.form.get(fieldName);
        return field.invalid && (field.touched || this.submitAttempted);
    }

}
template.ts(user form.component.html)


{{正在加载?'Saving':'Add x'}}
改为执行此操作
(submit)=“submitForm()”
。您可以在
submitForm
功能中检查
form.valid

要提交表单,您需要在表单上使用ngSubmit,并且要启用/禁用表单中的提交按钮,我们可以这样检查:form.valid并将其绑定到按钮禁用属性。
To Submit a form you need to use ngSubmit on the form, and to enable/disable the submit button in the form, we can check like this: form.valid and bind this to the button disabled attribute.

Something like this:
<form [formGroup]="form" (ngSubmit)="submitForm()">
<button type="submit" [disabled]='!form.valid'>Submit</button>
</form>
大概是这样的: 提交
您会收到该错误,因为在表单类中,submitForm希望表单作为参数,但在模板中您没有传递表单


固执己见的解决方案:不要这样做。只需使用submitForm()在表单上触发验证。没有理由将表单实例交还给它自己。

从组件代码中删除它

submitForm(form: NgForm) { }
传递的参数本身不是必需的,因为您已将表单与组件和模板链接。这要求您也在模板中传递参数。但在我的情况下,将其从组件中删除将100%起作用

<form #f="ngForm" id="someForm" name="form" (ngSubmit)="f.form.valid && buyTicket(ticket)" novalidate>
这意味着在
.ts
中,文件必须是没有参数的错误函数-
buyTicket()

我的意思是,不仅表单实例会产生此错误,它也可能是任何有缺陷的函数。

你是说……submitForm(form:NgForm){}应该是submitForm(){}?是的。正在执行的代码是创建表单实例的表单控制器。无需将表单实例传递回控制器,因为它已绑定。
submitForm(form: NgForm) { }
<form #f="ngForm" id="someForm" name="form" (ngSubmit)="f.form.valid && buyTicket(ticket)" novalidate>
ERROR in src\app\event\event.component.html(23,54): : Expected 0 arguments, but got 1.