gpt4 book ai didi

"this.users = users;" not binding to "users;" property(“this.users=用户;”不绑定到“用户;”属性)

转载 作者:bug小助手 更新时间:2023-10-22 16:38:52 26 4
gpt4 key购买 nike



export class UserListComponent implements OnInit{
users;

constructor(private userService: UserService) {}

ngOnInit(){
this.userService.getUsers()
.subscribe(users=> {
console.log(users);
this.users = users;
});
}
}

The "users;" property above displays a
"Member 'users' implicitly has an 'any' type.ts(7008)
(property) UserListComponent.users: any"
error. help me

上面的“users;”属性显示“成员“users”隐式具有“any”类型。ts(7008)(property)UserListComponent.users:any”错误。帮帮我


This is an angular CLI project leveraging the github "https://api.github.com/users" api.

这是一个利用github的有角度的CLI项目“https://api.github.com/users“api。


更多回答

So what is the type of users? It should be something like users: UserModel[]; or similar. Worst case, you could do users: any;, but any should be avoided.

那么用户的类型是什么呢?它应该类似于users:UserModel[];或类似的。最坏的情况是,你可以做用户:任何;,但是应该避免任何情况。

优秀答案推荐

The quick fix is to change users; to users: any;, if you don't care about typings.

快速解决办法是更换用户;对用户:任何;,如果你不在乎打字员的话。


If you do care about typings you should type users the same as the return value of getUsers.subscribe().

如果您确实关心键入,那么您应该键入与getUsers.subscribe()的返回值相同的user。


Assuming getUsers() returns something along the lines of Observable<User[]>,
your initial declaration of users could be users: User[] = []; or users: User[] | null = null;

假设getUsers()返回的内容与Observable<User[]>类似,那么用户的初始声明可能是users:User[]=[];或users:User[]| null=null;



this.userService.getUsers()
subscribe((users:any) => {
console.log(users);
this.users = users;
});

Modify like this while subscribing data from observable.

在订阅observable的数据时这样修改。


Add any (users:any) as type for incoming response from observable.

添加any(users:any)作为来自observable的传入响应的类型。



When TypeScript is unable to infer a variables/members type because there are no type annotations present it sometimes falls back to the type any. This compiler option is enabled per default.

当TypeScript由于没有类型注释而无法推断变量/成员类型时,它有时会返回到类型any。默认情况下启用此编译器选项。


To prevent this error:

要防止此错误,请执行以下操作:



  • Set set an explicit Type Annotation for users. users is likely going to be something like User[].

  • Disable the noImplicitAny compiler option. (Not recommended)



Change users; to

更改用户;到


users: any[]=[];

users:any[]=[];


or to


users: any;

用户:任意;


更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com