我在使用Ionic构建任务列表时正在关注Angular英雄教程。我在本教程的“ http”部分,但无法使用RxJS删除任务。
发生了什么:
我首先怀疑这与Ionic有一些冲突,但是即使我将滑动操作更改为常规单击事件,也会发生相同的情况。
以下是相关组件的代码。任何帮助将不胜感激!
您可以在此处找到代码:https://github.com/hktang/butler
<ion-header>
<ion-toolbar>
<ion-title>Tasks</ion-title>
</ion-toolbar>
</ion-header>
<ion-content padding>
<ion-list>
<app-task-input [tasks]="tasks"></app-task-input>
<app-task-list
[tasks]="tasks"
(selectedTaskChange)="onSelectedTaskChange($event)"
></app-task-list>
</ion-list>
<app-task-detail [task]="selectedTask"></app-task-detail>
<app-messages></app-messages>
</ion-content>
import { Component, OnInit } from "@angular/core";
import { Task } from "./task";
import { TaskService } from "./task.service";
@Component({
selector: "app-tasks",
templateUrl: "./tasks.page.html",
styleUrls: ["./tasks.page.scss"]
})
export class TasksPage implements OnInit {
selectedTask: Task;
tasks: Task[];
constructor(private taskService: TaskService) {}
ngOnInit() {
this.getTasks();
}
getTasks(): void {
this.taskService.getTasks().subscribe(tasks => (this.tasks = tasks));
}
onSelectedTaskChange(task: Task): void {
this.selectedTask = task;
}
}
import { Injectable } from "@angular/core";
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { Observable, of } from "rxjs";
import { Task } from "./task";
import { MessageService } from "../messages/message.service";
import { catchError, map, tap } from "rxjs/operators";
const httpOptions = {
headers: new HttpHeaders({ "Content-Type": "application/json" })
};
@Injectable({
providedIn: "root"
})
export class TaskService {
private tasksUrl = "api/tasks";
constructor(
private http: HttpClient,
private messageService: MessageService
) {}
...
/** DELETE: delete the hero from the server */
deleteTask(task: Task | number): Observable<Task> {
const id = typeof task === 'number' ? task : task.id;
const url = `${this.tasksUrl}/${id}`;
return this.http.delete<Task>(url, httpOptions).pipe(
tap(_ => this.log(`deleted task id=${id}`)),
catchError(this.handleError<Task>('deleteTask'))
);
}
...
}
<ion-list *ngIf="tasks">
<ion-item-sliding
*ngFor="let task of tasks"
(ionSwipe)="markDone(task)">
<ion-item
(click)="onSelect(task)"
[class.selected]="task === selectedTask">
<ion-label>{{task.name}}</ion-label>
</ion-item>
<ion-item-options side="right">
<button ion-button (click)="markDone(task)">Done</button>
</ion-item-options>
</ion-item-sliding>
</ion-list>
import { Component, EventEmitter, OnInit, Input, Output } from "@angular/core";
import { Task } from "../task";
import { TaskService } from "../task.service";
@Component({
selector: "app-task-list",
templateUrl: "./task-list.component.html",
styleUrls: ["./task-list.component.scss"]
})
export class TaskListComponent implements OnInit {
selectedTask: Task;
@Input()
tasks: Task[];
@Output()
selectedTaskChange = new EventEmitter<Task>();
constructor(private taskService: TaskService) {}
ngOnInit() {}
onSelect(task: Task): void {
this.selectedTask = task;
this.selectedTaskChange.emit(task);
}
markDone(task: Task){
this.tasks = this.tasks.filter(tsk => tsk !== task);
this.taskService.deleteTask(task).subscribe();
}
}
答案 0 :(得分:1)
将显示任务的代码更改为此即可解决问题
<ion-item-sliding *ngFor="let task of tasks" (ionSwipe)="markDone(task)">
<ion-item>{{task.name}}</ion-item>
<ion-item-options> </ion-item-options>
</ion-item-sliding>