typescript Angular 2 用空值实例化类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43783031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular 2 instantiate class with empty values
提问by Daniel S
How do you instantiate the members as class with empty values?
如何将成员实例化为具有空值的类?
I instantiate with
我实例化
public members = new MemberClass();
public members = new MemberClass();
but the console shows Members: {} so I'm unable to set the default empty values
但控制台显示成员:{} 所以我无法设置默认的空值
so basically I'm trying to replicate an empty structure like:
所以基本上我试图复制一个空结构,如:
this.members = [
{
"Name" : "", "Id" : "", "Team" : "",
"Cases" : {
"history" : []
}
}
];
回答by AJT82
Use interfaces ;) And this is a way you can create an Array with an empty object of type Member
with properties set. I don't know the use case, at least I do not set the properties for empty objects/arrays of models, but here we go :)
使用接口 ;) 这是一种您可以创建一个类型为空对象Member
并设置了属性的数组的方法。我不知道用例,至少我没有为空对象/模型数组设置属性,但我们开始了:)
So first your interfaces, which are:
所以首先你的接口是:
export interface Member {
Id: string;
Name: string;
Cases: Case[];
}
export interface History {
Id: string;
CaseNumber: string;
}
export interface Case {
History: History[];
}
Then you create 3 functions to set the properties:
然后创建 3 个函数来设置属性:
export function createMember(Id?:string,Name?:string, Cases?:Case[]): Member {
return {
Id,
Name,
Cases[createCase()]
}
}
export function createCase(History?:History[]): Case {
return {
History[createHistory()]
}
}
export function createHistory(Id?: string,CaseNumber?:string): History {
return {
Id,
CaseNumber
}
}
And now in your component you can call the function createMember
, that then calls the two other functions:
现在在您的组件中,您可以调用 function createMember
,然后调用另外两个函数:
members = [createMember()];
PS, according to your models, the following is not the model of Member
-array, they name and build doesn't match, but I have created the above, based on the models you have provided.
PS,根据您的模型,以下不是Member
-array的模型,它们的名称和构建不匹配,但是我根据您提供的模型创建了上述模型。
回答by aaron-bond
Keep in mind that you're using Interfaces there, not classes. In Typescript, as in Javascript, an empty object is just {}
请记住,您在那里使用的是接口,而不是类。在 Typescript 中,就像在 Javascript 中一样,空对象只是{}
So, to do what you're looking for, try this:
let history: History = { Id:"", CaseNumber:""};
因此,要执行您要查找的操作,请尝试以下操作:
let history: History = { Id:"", CaseNumber:""};
If you try to leave out any of those properties, you'll get an error that they're required. If you want properties of an interface to be optional when instantiating, update your interface like this: CaseNumber?: string;
如果您尝试忽略这些属性中的任何一个,您将收到一个错误,指出它们是必需的。如果您希望在实例化时接口的属性是可选的,请像这样更新您的接口:CaseNumber?: string;
To further clarify: a class is a concrete type that you can instantiate and use as you would in any language. If you need a constructor or default values on your properties, you should use export class MyType {}
.
进一步澄清:类是一种具体类型,您可以像在任何语言中一样实例化和使用它。如果您的属性需要构造函数或默认值,则应使用export class MyType {}
.
However, in my experience, I've found interfaces in Typescript to be more useful than classes in many cases. The reason for this is that you can code against them at dev-time, but they aren't included in your bundles when you ship (N.B. this ENTIRELY depends on your use-case).
但是,根据我的经验,我发现 Typescript 中的接口在许多情况下比类更有用。这样做的原因是您可以在开发时针对它们进行编码,但是在您发货时它们不包含在您的捆绑包中(注意这完全取决于您的用例)。