这是我的第一个Angular项目,我一直使用内置服务器,并偶尔进行构建以推送到服务器 - 现在我正在尝试进行--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个。
所以我想提供完整的组件和模板。
component.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>