在Typescript接口和类之间有什么不同?我什么时候使用Class?我什么时候使用Interfaces?他们有什么好处?
我需要为我的后端服务器(使用Angular 2执行它)创建某种类型的http请求,例如:},
"fields": {
"project": {
"id": "10000"
},
"summary": "something's wrong",
"issuetype": {
"id": "10000"
},
"assignee": { // not neccesary required
"name": "homer"
},
"reporter": {
"name": "smithers"
},
"priority": { // not neccesary required
"id": "20000"
}
}
Run Code Online (Sandbox Code Playgroud)
我应该用什么来构建这些模型?谢谢!
我有一个简单的登录方案,要求用户在Typescript中输入电子邮件和密码.我需要创建一些类型来获得强类型并将其发送到后端.
应该写成:
export interface UserLogin {
email: string;
password: string;
}
//OR
export class UserLogin {
email: string;
password: string;
}
Run Code Online (Sandbox Code Playgroud)
以及如何知道何时使用这些方案?
我现在真的很困惑,因为我得到了ERROR TypeError: "_this.device.addKeysToObj is not a function".但我实现了这个功能,所以我不知道问题是什么或为什么它不可调用.我已经尝试使用Firefox和chrome的代码,两者都是通过相同的错误.
错误符合 this.device.addKeysToObj(this.result.results[0]);
这是我的班级:
export class Device {
id: number;
deviceID: string;
name: string;
location: string;
deviceType: string;
subType: string;
valueNamingMap: Object;
addKeysToObj(deviceValues: object): void {
for (let key of Object.keys(deviceValues).map((key) => { return key })) {
if (!this.valueNamingMap.hasOwnProperty(key)) {
this.valueNamingMap[key] = '';
}
}
console.log(this, deviceValues);
}
}
Run Code Online (Sandbox Code Playgroud)
这就是电话:
export class BatterieSensorComponent implements OnInit {
@Input() device: Device;
public result: Page<Value> = new Page<Value>();
//[..]
ngOnInit() {
this.valueService.list('', this.device).subscribe(
res => …Run Code Online (Sandbox Code Playgroud)