如何分配A'功能到模型中的变量。打字稿
代码下面:
export class User {
constructor(
public Id: number,
public Prefix: string,
public FirstName: string,
public LastName: string,
public MiddleInitial: string,
public FullName: string = function() {
return FirstName + ' ' + LastName;
},
){ }
}
全名变量正在抛出错误,任何帮助/其他方法都将不胜感激。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您只想在班上有一个属性Getter。
类似:
Playground
It sounds like you just want a property getter on your class.
Something like:
Playground
您声明
fullname
应该期望字符串,但您尝试为其分配功能。正如您要分配给两个构造函数的组成的fullName值一样身体:
You state that
FullName
should expect string, but you try to assign function to it.As you want to assign to FullName value that will be a composition of two constructor params, you have to declare
FullName
as aUser
class field, and assign value to it in constructor body: