Angular - 使用AoT编译的类型“{}”上不存在属性“电子邮件”

时间:2017-05-13 14:44:21

标签: angular

我在ng中遇到错误错误:... template.html:使用Angular Cli>进行编译时,类型“{}”上的属性“电子邮件”不存在。 ng build --prod --aot

表单模板:

        <form (ngSubmit)="login()" #loginForm="ngForm">

            <input [(ngModel)]="user.email" id="email"
                   type="email" class="validate" name="email" required>
            <label for="email">Email</label>

            ...

        </form>

LoginComponent:

export class LoginComponent {

    public user = {};
    public errorMsg = '';
    email: any;
    password: any;

constructor(
...
) {

添加类型电子邮件:字符串或电子邮件:表单组件中的任何内容都不起作用。它似乎指的是工厂Angular组件{}。我怎样才能解决这个错误?

3 个答案:

答案 0 :(得分:2)

public user = {};

此用户对象没有任何名为email的属性。将其更改为

public user = { email: '' }; 

例如。

答案 1 :(得分:1)

您的问题来自用户实例化。

以下实例化应该有效:

public user = {'email': ''}

但是密码可能会出现类似的错误(具体取决于您在模板中的使用方式)。

在这种情况下,实例化应该是:

public user = {'email': '',
               'password': ''
              }

更好的方法是创建用户类。 user.ts应如下所示:

export class User {

    constructor(public email?: string,
                public password?: string) { }

}

然后在LoginComponent中实例化用户,如下所示:

public user: User = new User();

答案 2 :(得分:0)

您也可以指定一种对象。

public user = <{email: string}>{};