属性 'todos' 在类型 'Object' 上不存在
Property 'todos' does not exist on type 'Object'
我正在构建我的第一个 MEAN 堆栈来做应用程序。我希望我能正确地表达我的问题,因为我是新手。
错误来自我的 getList().subscribe 函数。我想我明白错误试图告诉我什么,但我不知道如何解决它。我试过分配
todos: any;
但这没有用。然后我尝试分配
todos: Array<any>;
和许多其他不同的语法;没有运气。
dashboard.component.ts
export class DashboardComponent implements OnInit {
user: Object;
lists: any;
todos: any;
@Input() input;
@Output() update = new EventEmitter();
constructor(
private authService: AuthService,
private flashMessage: FlashMessagesService,
private listService: ListService
) { }
ngOnInit() {
this.getLists();
this.authService.getProfile().subscribe(profile => {
this.user = profile.user;
},
err => {
console.log(err);
return false;
});
}
getLists(): void {
this.listService.getAllTodos()
.subscribe(data => this.lists = data.todos);
}
list.service.ts
export class ListService {
newTodo: any;
list: any;
constructor(private http: HttpClient) { }
getAllTodos() {
return this.http.get('http://localhost:3000/todos/lists');
}
编辑 ================================== ================
这是我的自定义 interface/class 模型,我尝试更改编码以订阅 observable
List.ts
export class List {
item: string;
completed: boolean;
}
更改为我的旧文件
dashboard.component.ts
import { List } from '../../list';
export class DashboardComponent implements OnInit {
lists: List;
getLists(): void {
this.listService.getAllTodos()
.subscribe((data: List) => this.lists = data.todos);
}
除了类型从 'Object' 更改为 'List'
外,我仍然在主题行中收到相同的错误
Property 'todos' does not exist on type 'List'
您可以自定义 interface/class
来映射响应。或者在访问 todos 之前使用 any 类型。
this.listService.getAllTodos()
.subscribe((data:any) => this.lists = data.todos);
我正在构建我的第一个 MEAN 堆栈来做应用程序。我希望我能正确地表达我的问题,因为我是新手。
错误来自我的 getList().subscribe 函数。我想我明白错误试图告诉我什么,但我不知道如何解决它。我试过分配
todos: any;
但这没有用。然后我尝试分配
todos: Array<any>;
和许多其他不同的语法;没有运气。
dashboard.component.ts
export class DashboardComponent implements OnInit {
user: Object;
lists: any;
todos: any;
@Input() input;
@Output() update = new EventEmitter();
constructor(
private authService: AuthService,
private flashMessage: FlashMessagesService,
private listService: ListService
) { }
ngOnInit() {
this.getLists();
this.authService.getProfile().subscribe(profile => {
this.user = profile.user;
},
err => {
console.log(err);
return false;
});
}
getLists(): void {
this.listService.getAllTodos()
.subscribe(data => this.lists = data.todos);
}
list.service.ts
export class ListService {
newTodo: any;
list: any;
constructor(private http: HttpClient) { }
getAllTodos() {
return this.http.get('http://localhost:3000/todos/lists');
}
编辑 ================================== ================
这是我的自定义 interface/class 模型,我尝试更改编码以订阅 observable
List.ts
export class List {
item: string;
completed: boolean;
}
更改为我的旧文件
dashboard.component.ts
import { List } from '../../list';
export class DashboardComponent implements OnInit {
lists: List;
getLists(): void {
this.listService.getAllTodos()
.subscribe((data: List) => this.lists = data.todos);
}
除了类型从 'Object' 更改为 'List'
外,我仍然在主题行中收到相同的错误Property 'todos' does not exist on type 'List'
您可以自定义 interface/class
来映射响应。或者在访问 todos 之前使用 any 类型。
this.listService.getAllTodos()
.subscribe((data:any) => this.lists = data.todos);