Angular2 表单中的 TypeScript 编译问题 [英] Issue with TypeScript compilation in Angular2 Forms

查看:37
本文介绍了Angular2 表单中的 TypeScript 编译问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Angular2 中定义了一个表单:

I have defined a form in Angular2 like that:

    this.form = this._formBuilder.group({
        password: ['',Validators.required],
        passwordRepeat: ['',Validators.required]
    });

哪里

public form:ControlGroup

这很好,因为:

 _formBuilder = FormBuilder.group(controlsConfig: {
        [key: string]: any;
    }, extra?: {
        [key: string]: any;
    }): modelModule.ControlGroup

它返回ControlGroup.

现在,在我使用的组件中:

Now, in my component I am using:

this.user.password = this.passwordEditForm.controls.password.value;

这会引发我的编译错误:

Which throws me compilation error of:

error TS2339: Property 'password' does not exist on type '{ [key: string]: AbstractControl; }'.

好像是个bug.关于如何克服这个问题的任何想法?我试过这样做:

Seems like a bug. Any ideas on how I can overcome this issue? I've tried doing so:

export interface FormControlGroup extends ControlGroup{
password:any;
}

但这给我带来了更多错误:

But this gives me even more errors:

error TS1206: Decorators are not valid here.
app/form.component.ts(30,9): error TS2322: Type 'ControlGroup' is not assignable to type 'FormControlGroup'.
  Property 'password' is missing in type 'ControlGroup'.
app/form.component.ts(37,61): error TS2339: Property 'password' does not exist on type '{ [key: string]: AbstractControl; }'.

推荐答案

替换以下内容:

this.user.password = this.passwordEditForm.controls.password.value;

this.user.password = this.passwordEditForm.controls['password'].value;

它对我有用.我认为这只是一种解决方法.

It works for me. I thinks it's only a workaround.

这篇关于Angular2 表单中的 TypeScript 编译问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆