[英]How to set optional class parameters in Angular 2-Typescript?
我知道在Typescript中可选参数可以用问号标记 。
但是,我发现实际使用new关键字实例化类的唯一方法。
问题是基于Angular 2的首发“英雄”教程类没有通过new关键字实例化,据我所知,这是由Angular内部完成的。
例如,我有这个代码:
车型/ users.ts
export class User {
id: number;
name: string; // I want this to be optional
}
模型/模拟users.ts
import {User} from './user';
export var USERS: User[] = [
{
id: 1
// no name (I wanted it to be optional for user with id 1)
},
{
id: 2,
name: "User 2"
},
]
服务/ user.service.ts
import {Injectable} from 'angular2/core';
import {USERS} from './../models/mock-users';
@Injectable()
export class UserService {
getUsers() {
return Promise.resolve(USERS);
}
}
意见/我-component.component.ts
// imports here...
@Component({
// ...
})
export class MyComponent {
constructor(private _userService: UserService) { }
getUsers() {
this._userService.getUsers().then(users => console.log(users));
}
}
在您的情况下,使用User的界面可能更方便。
export interface User {
id: number;
name?: string; // interfaces allow fields to be optional
}
当我想要定义对象的'形状'时,我需要使用接口,当需要添加行为(方法)时,我会使用类。 如果您只需要移动数据,我就会使用一个界面。
如果确实需要一个类,那么在mock-users.ts
创建Users实例的语法应该有点不同。 首先,TypeScript中没有“可选类字段”。 任何字段都不能设置/'未定义',因此没有意义标记字段可选。 您可以使用new
关键字实例化一个类 - 缺点是您需要编写构造函数来设置字段值或将实例分配给变量并设置字段。 但是使用new关键字没有任何问题,特别是对于测试对象。
您也可以像在mock-users.ts
那样使用对象文字实例化对象 - 您需要通过添加mock-users.ts
转换来更明确。
export var USERS: User[] = [
<User> { // explicit cast to let the TypeScript compiler know you know what you're doing
id: 1
// no name (I wanted it to be optional for user with id 1)
},
{
id: 2,
name: "User 2"
},
]
如果没有强制转换,TypeScript编译器将产生错误。 这是设计上的错误。 如果您想了解更多有关(有趣)的原因,请参阅以下关于类型检查背后的一些思考的相关详细讨论:
class User {
constructor(public id: number, public name: string = null) {}
}
var USERS: User[] = [
new User(1),
new User(2, 'User 2')
];
console.log(USERS);
使用构造函数可能会让您的生活更轻松。 你不会得到undefined
,当你试图得到一个用户名,你就可以在类中定义的功能。 唯一的区别是您使用new User(...)
而不是仅使用对象文字作为类型。
另一种方法是使用Object.assign
使用您只需要的属性扩展有效的类型对象(在此示例中省略属性a
和c
)
export class A {
a:number=1;
b:number;
c:string;
d:string;
}
let validA:A = Object.assign(new A(),{
b:3,
d:'Lorem ipsum'
});
我个人更喜欢这种语法,以避免多线对象初始化和无聊的事情,为我的应用程序的每个模型创建一个接口(如果存在相应的类,所以另一个文件是无用的)。
此外,请毫不犹豫地在类定义中设置默认值,即使这种情况不是强制性的。
最后一个重要的事情是,在这种情况下你不会松散类方法(反对{}转换)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.