Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 应该存在的未定义函数?_Angular_Typescript - Fatal编程技术网

Angular 应该存在的未定义函数?

Angular 应该存在的未定义函数?,angular,typescript,Angular,Typescript,我有一个类型为“user”的对象,它应该有一个函数“getPermission()”。在运行期间(7),它抛出错误“TypeError:this.user.getPermission不是函数” 在我看来,这是最基本的背景: 我的用户类: export class User { ... public getPermission(): boolean { return true; } ... } 我有一个从api(即asp.net核心)获取用户的服务: 如上所

我有一个类型为“user”的对象,它应该有一个函数“getPermission()”。在运行期间(7),它抛出错误“TypeError:this.user.getPermission不是函数”

在我看来,这是最基本的背景:

我的用户类:

export class User  {
   ...
   public getPermission(): boolean {
      return true;
   }
   ...
}
我有一个从api(即asp.net核心)获取用户的服务:

如上所述,我得到一个错误:

TypeError: this.user.getPermission is not a function
除了函数和运行时不存在的所有其他函数之外,从api加载的所有属性都在那里


我将非常感谢任何一个打击,这是如何发生的

创建类的实例

user: User = new User();

TypeScript仅在编译时存在。它在运行时不做任何事情

简单地执行
this.http.get(…)
不会将您收到的任何内容转换为您指定的类
User


您需要自己解析输入并实例化
用户
实例。

嗨,unional,我试图理解您的意思,但是。。。为什么不解析响应?我应该在哪里实例化用户实例?谢谢你的帮助!将响应强制转换为用户时,用户类不会实例化。TypeScript只在JavaScript上装饰其类型系统。编译为JavaScript时,将删除所有类型信息。没有魔法。您需要
new User()
并自己填写响应中的属性。嗨,unional,谢谢您的帮助。我找到了类转换器,这个工作很好!如果你有任何建议,如果有更好的东西在那里,谢谢让我知道。我尝试了这个(在组件中),但仍然有相同的结果!你是说这个吗?用户:用户=新用户();ngOnInit(){this.route.data.subscribe(data=>{this.user=data['user'];});const t:boolean=this.user.getPermission();}
 user: User;

 ngOnInit() {

   this.route.data.subscribe(data => {
     this.user = data['user'];
   });

   const t: boolean = this.user.getPermission();
 }
TypeError: this.user.getPermission is not a function
user: User = new User();