gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:57:29 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com