[英]Angular 7: Property 'data' does not exist on type 'Object'
I am using angular7 and following the step as the exact example here https://coursetro.com/posts/code/171/Angular-7-Tutorial---Learn-Angular-7-by-Example我正在使用 angular7 并按照此处的确切示例步骤https://coursetro.com/posts/code/171/Angular-7-Tutorial---Learn-Angular-7-by-Example
But getting the error data doesn't.但是获取错误数据却没有。 Here is the error below:
这是下面的错误:
src/app/home/home.component.html:18:29 - error TS2339: Property 'data' does not exist on type 'Object'.
18 <li *ngFor="let user of users.data">
~~~~~~~~~~
src/app/home/home.component.ts:6:16
6 templateUrl: './home.component.html',
~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component HomeComponent.
I am trying to test the code and generate the service but i keep getting errors in home.component.html home.component.ts我正在尝试测试代码并生成服务,但在 home.component.html home.component.ts 中不断出现错误
import { Component, OnInit } from '@angular/core';
import { DataService } from '../data.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
users: Object;
constructor(private data: DataService) { }
ngOnInit() {
this.data.getUsers().subscribe(data => {
this.users = data
console.log(this.users)
})
}
}
home.component.html home.component.html
<ul *ngIf="users">
<li *ngFor="let user of users.data">
<!-- <img [src]="user.avatar">
<p>{{ user.first_name }} {{ user.last_name }}</p> -->
<p> {{user}}</p>
</li>
</ul>
data.service.ts数据服务.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(private http:HttpClient) { }
getUsers() {
return this.http.get('https://reqres.in/api/users');
}
}
this is the exact code as the example..how can this be resolved?这是作为示例的确切代码..如何解决?
Define type for the user object like below.为用户 object 定义类型,如下所示。
user: {
data: any;
}
Then initialize user object with and empty array in HomeComponent.然后用 HomeComponent 中的空数组初始化用户 object。
try this尝试这个
users: any;
or make sure User, Data Object或确保用户,数据 Object
export class User {
bla : string,
data : Array<Data>
}
export class Data{
blabla : string,
blablabla : number
}
something like this像这样的东西
then然后
users : User = new User()
i hope it helps我希望它有帮助
We can see that you've typesafed users
as Object
.我们可以看到您已将
users
键入为Object
。 However, data
does not exist on the generic class Object
.但是,通用 class
Object
上不存在data
。 What you can do is just cast users
differently.您可以做的只是以不同的方式投射
users
。
users: {
data: {
first_name: string;
last_name: string;
}[];
}
So as you can see we're describing an object called users
with a property data
.如您所见,我们正在描述一个名为
users
的 object ,其属性为data
。 data
is an array of objects ( {}[]
) that contain a first_name
and last_name
property. data
是一个包含first_name
和last_name
属性的对象数组 ( {}[]
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.