Я продолжаю получать эту странную ошибку. Я уже задавал этот вопрос раньше, но предыдущее решение не помогло устранить проблему. Я приложил все отредактированные фрагменты кода по этой проблеме и могу опубликовать больше по запросу. Я новичок в angular, так что терпите меня. Спасибо!
Сообщение об ошибке
Error: src/app/pages/task-view/task-view.component.ts:27:60 - error TS2769: No overload matches this call.
Overload 1 of 5, '(observer?: NextObserver<Task> | ErrorObserver<Task> | CompletionObserver<Task> | undefined): Subscription', gave the following
error.
Argument of type '(tasks: Task[]) => void' is not assignable to parameter of type 'NextObserver<Task> | ErrorObserver<Task> | CompletionObserver<Task> | undefined'.
Property 'complete' is missing in type '(tasks: Task[]) => void' but required in type 'CompletionObserver<Task>'.
Overload 2 of 5, '(next?: ((value: Task) => void) | undefined, error?: ((error: any) => void) | undefined, complete?: (() => void) | undefined): Subscription', gave the following error.
Argument of type '(tasks: Task[]) => void' is not assignable to parameter of type '(value: Task) => void'.
Types of parameters 'tasks' and 'value' are incompatible.
Type 'Task' is missing the following properties from type 'Task[]': length, pop, push, concat, and 26 more.
27 this.taskService.getTasks(params.listId).subscribe((tasks: Task[]) => {
~~~~~~~~~~~~~~~~~~~~
node_modules/rxjs/internal/types.d.ts:64:5
64 complete: () => void;
~~~~~~~~
'complete' is declared here.
src/app/pages/task-view/task-view.component.ts:34:42 - error TS2769: No overload matches this call.
Overload 1 of 5, '(observer?: NextObserver<List> | ErrorObserver<List> | CompletionObserver<List> | undefined): Subscription', gave the following
error.
Argument of type '(lists: List[]) => void' is not assignable to parameter of type 'NextObserver<List> | ErrorObserver<List> | CompletionObserver<List> | undefined'.
Property 'complete' is missing in type '(lists: List[]) => void' but required in type 'CompletionObserver<List>'.
Overload 2 of 5, '(next?: ((value: List) => void) | undefined, error?: ((error: any) => void) | undefined, complete?: (() => void) | undefined): Subscription', gave the following error.
Argument of type '(lists: List[]) => void' is not assignable to parameter of type '(value: List) => void'.
Types of parameters 'lists' and 'value' are incompatible.
Type 'List' is missing the following properties from type 'List[]': length, pop, push, concat, and 26 more.
34 this.taskService.getList().subscribe((lists:List[]) => {
~~~~~~~~~~~~~~~~~~~
node_modules/rxjs/internal/types.d.ts:64:5
64 complete: () => void;
~~~~~~~~
'complete' is declared here.
task-view.component.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { TaskService } from 'src/app/task.service';
import { MatDialog } from '@angular/material/dialog';
import { Task } from 'src/app/models/task.model';
import { List } from 'src/app/models/list.model';
@Component({
selector: 'app-task-view',
templateUrl: './task-view.component.html',
styleUrls: ['./task-view.component.scss']
})
export class TaskViewComponent implements OnInit {
lists?: List[];
tasks?: Task[];
constructor(private taskService: TaskService, private route: ActivatedRoute, private dialog: MatDialog) { }
ngOnInit(): void {
this.route.params.subscribe(
(params: Params) => {
//console.log(params);
this.taskService.getTasks(params.listId).subscribe((tasks: Task[]) => {
this.tasks = tasks;
})
}
)
this.taskService.getList().subscribe((lists:List[]) => {
console.log(lists);
this.lists = lists;
})
}
onTaskClick(task: Task) {
// We want to set the task to completed
this.taskService.completed(task).subscribe(() => {
console.log("Completed successfully");
})
}
}
task.service.ts
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { List } from './models/list.model';
import { Task } from './models/task.model';
import { WebRequestService } from './web-request.service';
@Injectable({
providedIn: 'root'
})
export class TaskService {
constructor(private webReqService: WebRequestService) { // this line is injecting the web-request service we created
}
createList(title: string): Observable<List> {
// We want to send a web request to create a List
return this.webReqService.post('lists', { title }); // this is returning a observable
}
getList(): Observable<List>{
return this.webReqService.get('lists');
}
getTasks(listId: string): Observable<Task> {
return this.webReqService.get(`lists/${listId}/tasks`);
}
createTask(title: string, listId: string): Observable<Task> {
// We want to send a web request to create a Task
return this.webReqService.post(`lists/${listId}/tasks`,{ title }); // this is returning a observable
}
completed(task: Task): Observable<Task> {
return this.webReqService.patch(`lists/${task._listId}/tasks/${task._id}`,{
completed: true // maybe could be set to !completed?
} )
}
}
Функция getTasks в вашем сервисе возвращает ‹Task›, но вы пытаетесь получить тип ‹Task []› в вашем компоненте, что неверно.
вы должны соответствовать типам.