interface - Angular2 beta,一直使用接口(interface)未定义的属性

标签 interface angular

export interface User {
    country_name: string,
    password:     string,
    email:        string,
}

// login.conponent
import {Component} from 'angular2/core';
import {User} from "../../interfaces/user";
import {OnInit} from "angular2/core";

@Component({
    selector:'login',
    templateUrl:'app/components/login/login.component.html',
})
export class LoginComponent implements OnInit {

    newUser: User;

    constructor() {}

    onSubmit(email = "",password = "") {
       this.newUser.email = email; // as example        
    }

    ngOnInit() {

    }
}

任何时候当我尝试使用 newUser 时,它都会给我写 undefind,不知道我会调用哪一边,总是异常(exception)。此外,如果尝试查看 ngInit,它也会取消定义。 我错过了什么?

最佳答案

你为用户定义了界面

export interface UserInterface {
    country_name: string,
    password: string,
    email: string,
}

然而,如果你想使用它,你仍然需要创建 User 实例。

export class App {

    newUser: User;

    constructor() {
        this.newUser = new User();
        this.newUser.email = 'test@asd.com'
    }
}

为此,确保 User 类实现了 UserInterface:

export class User implements UserInterface {}

关于interface - Angular2 beta,一直使用接口(interface)未定义的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35361722/

相关文章:

c# - 您能用非常简单的术语解释一下 Moq 在这里做什么吗?

java - 在实现中使用接口(interface)类型与公共(public)接口(interface)

go - 为什么接口(interface)分配比方法调用更严格?

javascript - Angular 2 和 Express 日期格式 MongoDB

go - 如何创建具有多种返回类型的接口(interface)方法

javascript - 访问 Angular 分量元数据

angular - 如何将 Angular Material mat-card 切换为全屏

Angular 2+ : Testing form with mat-slide-toggle - change event won't fire

checkbox - Angular 2 polymer 和 PaperChechboxSelectedDirective

使用接口(interface)的 Java 泛型方法