Angular 无法读取属性';推动';当我调用this.users时未定义的
调用push方法时出错Angular 无法读取属性';推动';当我调用this.users时未定义的,angular,typescript,angular2-directives,Angular,Typescript,Angular2 Directives,调用push方法时出错 ng:///AppModule/ManageUser_Host.ngfactory.js:5 ERROR TypeError: Cannot read property 'push' of undefined at ManageUser.push../src/app/Views/user/user.component.ts.ManageUser.getUsers (main.js:7277) at ManageUser.push../src/app/Vie
ng:///AppModule/ManageUser_Host.ngfactory.js:5 ERROR TypeError: Cannot read property 'push' of undefined
at ManageUser.push../src/app/Views/user/user.component.ts.ManageUser.getUsers (main.js:7277)
at ManageUser.push../src/app/Views/user/user.component.ts.ManageUser.ngOnInit (main.js:7271)
at checkAndUpdateDirectiveInline (vendor.js:73109)
at checkAndUpdateNodeInline (vendor.js:74633)
at checkAndUpdateNode (vendor.js:74576)
at debugCheckAndUpdateNode (vendor.js:75469)
at debugCheckDirectivesFn (vendor.js:75410)
at Object.eval [as updateDirectives] (ng:///AppModule/ManageUser_Host.ngfactory.js:9)
at Object.debugUpdateDirectives [as updateDirectives] (vendor.js:75395)
at checkAndUpdateView (vendor.js:74542)
代码如下所示:
public users: User[];
this.users.push({"UserId":2,"FirstName":"string","FullName":"string","LastName":"string","EmailAddress":"string",
"GEID":"string",
"RITSID":"string",
"SOEID":"string",
"RoleID": 1,
"RoleName":"string",
"IsActive":true,
"ModifyingUserId":"string",
"CreatedBy":"string",
"CreatedDate":"string",
"EmailID":"string",
"Name": "string",
"Email": "string",
"Roles":"any",
"Manager":"string"});
console.log(this.users);
您只是在定义它,因此默认情况下该值为
未定义
public users: User[];
只需添加一个值,然后重试
public users: User[]= [];
快乐编码:) 您需要首先创建空数组,请尝试公共用户:User[]=[];值得在发布问题之前进行搜索。。。指的是副本。