Angular2 beta,始终使用接口未定义属性 导出用户界面{ 国家/地区名称:字符串, 密码:string, 电子邮件:string, } //login.conponent 从'angular2/core'导入{Component}; 从“../../interfaces/User”导入{User}; 从“angular2/core”导入{OnInit}; @组成部分({ 选择器:'login', templateUrl:'app/components/login/login.component.html', }) 导出类LoginComponent实现OnInit{ 新用户:用户; 构造函数(){} onSubmit(电子邮件=”,密码=”){ this.newUser.email=email;//例如 } 恩戈尼尼特(){ } }
任何时候当我尝试使用newUser时,它都会将我写为未查找,不知道我将调用哪一方,一直都是例外。此外,如果尝试查看ngInit,它也会取消查找。Angular2 beta,始终使用接口未定义属性 导出用户界面{ 国家/地区名称:字符串, 密码:string, 电子邮件:string, } //login.conponent 从'angular2/core'导入{Component}; 从“../../interfaces/User”导入{User}; 从“angular2/core”导入{OnInit}; @组成部分({ 选择器:'login', templateUrl:'app/components/login/login.component.html', }) 导出类LoginComponent实现OnInit{ 新用户:用户; 构造函数(){} onSubmit(电子邮件=”,密码=”){ this.newUser.email=email;//例如 } 恩戈尼尼特(){ } },interface,angular,Interface,Angular,任何时候当我尝试使用newUser时,它都会将我写为未查找,不知道我将调用哪一方,一直都是例外。此外,如果尝试查看ngInit,它也会取消查找。 我可以忽略什么?此的值。未定义newUser 您需要在指定任何属性值之前对其进行初始化,例如: this.newUser={country\u name:null,password:null,email:null}您为用户定义了接口 export interface UserInterface { country_name: string,
我可以忽略什么?此的
值。未定义newUser
您需要在指定任何属性值之前对其进行初始化,例如:
this.newUser={country\u name:null,password:null,email:null}
您为用户定义了接口
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 {}