typescript - Angular CLI 编译错误;预期有 1 个参数,但得到 0 个

标签 typescript angular-cli

这是我的第一个有角度的项目,我一直在运输,使用内置服务器,偶尔会进行构建以推送到服务器 - 现在我正在尝试 --prod 构建......并且我得到了编译错误。

周四,通过大量阅读,我已经成功解决了其中的大部分问题,并且我了解 AOT 需求,但是这个错误让我感到困惑

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

<form novalidate="novalidate"
    autocomplete="off"
    [formGroup]="form"
    (submit)="form.valid && submitForm()" >

但是在其中一些模板上我得到了

x.component.html(6,3): : Expected 1 arguments, but got 0.

其中第 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)

<form 
    novalidate="novalidate"
    autocomplete="off"
    [formGroup]="form"
    (ngSubmit)="submitForm()" >

    <label class="input" [class.state-error]="showError('x')">
        <input type="text" name="x" placeholder="x" formControlName="x" />
        <div *ngIf="xStatus" [innerHTML]="xStatus | safeHtml"></div>
    </label>


    <button type="submit" class="btn btn-primary" (click)="submitAttempted = true">
        <i *ngIf="loading" class='fa fa-spinner fa-spin '></i>
        {{ loading ? 'Saving' : 'Add x' }}
    </button>

</form>

最佳答案

您收到该错误是因为在表单类中,submitForm 需要表单作为参数,但在模板中您没有传递表单。

意见解决方案:不要这样做。只需使用submitForm() 来触发表单验证。没有理由将表单实例交还给其自身。

关于typescript - Angular CLI 编译错误;预期有 1 个参数,但得到 0 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49745378/

相关文章:

Angular 6 - 找不到模块 "@angular-devkit/build-angular"

Typescript 从对象中的数组创建字符串文字联合类型

node.js - 使用 Mocha 和 Chai 进行测试,消耗之前测试中的值

javascript - 如何在Angular2中观察第三方事件?

node.js - 角度 cli 安装命令显示错误

javascript - 任何人都可以帮助我理解运行 ngserve (Angular 8) 时 cmd 中的这条消息吗?

Angular AOT 编译失败(使用 Angular/compiler-cli)

node.js - npm run build 期间 docker build 中的 Typescript 错误

jquery - React 和 TypeScript - 如何在不包装在父 div 中的情况下进行渲染?

javascript - 尝试映射 Enum 键以生成 JSX 元素时出现 Typescript 错误