Angular 添加新项目时更新列表

Angular 添加新项目时更新列表,angular,typescript,angular-httpclient,Angular,Typescript,Angular Httpclient,使用Angular 7,我有以下服务(): 问题 问题是如何集成HttpClient以使所有内容同步: 因此,当创建新的Todo时,应更新Todo列表…保持同步的一种方法是使用RxJS操作符点击根据API的响应更新行为主体,例如: public get(): Observable<Todo[]> { return this.httpClient.get<Todo>(`todos`) .pipe(tap(todo => this.todos.next(t

使用Angular 7,我有以下服务():

问题

问题是如何集成HttpClient以使所有内容同步:


因此,当创建新的
Todo
时,应更新
Todo
列表…

保持同步的一种方法是使用RxJS操作符
点击
根据API的响应更新行为主体,例如:

public get(): Observable<Todo[]> {
  return this.httpClient.get<Todo>(`todos`)
     .pipe(tap(todo => this.todos.next(todos)))
}
public get():可观察{
返回此.httpClient.get(`todos`)
.pipe(点击(todo=>this.todos.next(todos)))
}

公共创建(todo):可观察{
返回此.httpClient.post(`apirl`,todo)
.pipe(点击(todo=>//使用todo执行某些操作))
}

保持同步的一种方法是使用RxJS操作符
点击
,根据API的响应更新行为主体,例如:

public get(): Observable<Todo[]> {
  return this.httpClient.get<Todo>(`todos`)
     .pipe(tap(todo => this.todos.next(todos)))
}
public get():可观察{
返回此.httpClient.get(`todos`)
.pipe(点击(todo=>this.todos.next(todos)))
}

公共创建(todo):可观察{
返回此.httpClient.post(`apirl`,todo)
.pipe(点击(todo=>//使用todo执行某些操作))
}

使用通知服务通知列表组件重新装入服务器

export class RepollTodosNotificationService {

  subject: ReplaySubject<any> = new ReplaySubject();
  obs: Observable<any> = this.subject.asObservable;

  notify = (data: any) => {
    this.subject.next(data)
  }
}
TodoCreateComponent

this.todoSevice.post(myNewTodo)
     .subscribe(
        result => {
          // current callback code
          this.repollNotifierService.notify(null); // null or data you want to send
export class TodoListComponent implements OnInit, OnDestroy {
    private repollSubscription: Subscription;

    constructor(private repollSvc: RepollTodosNotificationService) {}

    ngOnInit() {
       this.repollSvc.obs.subscribe(() => this.fetchTodos()); // if you transfer data, handle here
    }

    ngOnDestroy() {
        this.subscription.unsubscribe();
    }

    // methods

}
ToDoList组件中

this.todoSevice.post(myNewTodo)
     .subscribe(
        result => {
          // current callback code
          this.repollNotifierService.notify(null); // null or data you want to send
export class TodoListComponent implements OnInit, OnDestroy {
    private repollSubscription: Subscription;

    constructor(private repollSvc: RepollTodosNotificationService) {}

    ngOnInit() {
       this.repollSvc.obs.subscribe(() => this.fetchTodos()); // if you transfer data, handle here
    }

    ngOnDestroy() {
        this.subscription.unsubscribe();
    }

    // methods

}

使用通知服务通知列表组件重新装入服务器

export class RepollTodosNotificationService {

  subject: ReplaySubject<any> = new ReplaySubject();
  obs: Observable<any> = this.subject.asObservable;

  notify = (data: any) => {
    this.subject.next(data)
  }
}
TodoCreateComponent

this.todoSevice.post(myNewTodo)
     .subscribe(
        result => {
          // current callback code
          this.repollNotifierService.notify(null); // null or data you want to send
export class TodoListComponent implements OnInit, OnDestroy {
    private repollSubscription: Subscription;

    constructor(private repollSvc: RepollTodosNotificationService) {}

    ngOnInit() {
       this.repollSvc.obs.subscribe(() => this.fetchTodos()); // if you transfer data, handle here
    }

    ngOnDestroy() {
        this.subscription.unsubscribe();
    }

    // methods

}
ToDoList组件中

this.todoSevice.post(myNewTodo)
     .subscribe(
        result => {
          // current callback code
          this.repollNotifierService.notify(null); // null or data you want to send
export class TodoListComponent implements OnInit, OnDestroy {
    private repollSubscription: Subscription;

    constructor(private repollSvc: RepollTodosNotificationService) {}

    ngOnInit() {
       this.repollSvc.obs.subscribe(() => this.fetchTodos()); // if you transfer data, handle here
    }

    ngOnDestroy() {
        this.subscription.unsubscribe();
    }

    // methods

}

在我看来,处理此问题的最佳方法是在发布新TODO的请求的订阅回调中重新调用服务器。您的意思是,在TODOCREATE组件内部的todoService.create回调调用todoService.get并用更新的数据重新填充列表?但是如何才能访问ToDorCreateComponent访问TodoListComponent以更新列表。使用中所述的服务,只需一个更改-在
app.module
providers
数组中添加该服务,而不是在组件的
providers
元数据中列出该服务。您的意思是创建一个属性为“DataUpdated”的服务吗然后将该服务注入TodoListComponent和TodoCreateComponent?当TodoCreateComponent添加todo it更新时,该数据更新为true,并且TODOLISTCOMENT捕获该更改并调用TodoService Get以从API获取更新数据?这就是你的意思吗?请您发布一个简单的示例好吗?在我看来,处理这个问题的最好方法是在发布新TODO的请求的订阅回调中重新调用服务器。您的意思是,在TodoCreateComponent中的todoService.create回调调用todoService.get并用更新的数据重新填充列表?但是如何才能访问ToDorCreateComponent访问TodoListComponent以更新列表。使用中所述的服务,只需一个更改-在
app.module
providers
数组中添加该服务,而不是在组件的
providers
元数据中列出该服务。您的意思是创建一个属性为“DataUpdated”的服务吗然后将该服务注入TodoListComponent和TodoCreateComponent?当TodoCreateComponent添加todo it更新时,该数据更新为true,并且TODOLISTCOMENT捕获该更改并调用TodoService Get以从API获取更新数据?这就是你的意思吗?你能,请,只是张贴一个简单的例子吗?不确定我下面。。。您正在保持BehaviorSubject同步,但始终返回httpClient响应?你不应该回答“this.todos”?一个似乎有问题的问题。。。假设您的todoService.get接受一个名为“include”的参数。因此,可以使用“todos/?include=comments”、“todos/?include=comments,reviews”调用API。通过这样做,可以只使用基本数据或额外数据获取TODO。。。那不是个问题吗?this.todos将有一个todo列表,但其中一些包含额外数据?或者tap是否也会填充此.todos中现有TODO上缺少的属性?您可以将参数作为函数参数传递,这样会更灵活一些。您可以链接操作符,以便以您可能需要的任何方式映射数据。我不确定我在跟踪。。。您正在保持BehaviorSubject同步,但始终返回httpClient响应?你不应该回答“this.todos”?一个似乎有问题的问题。。。假设您的todoService.get接受一个名为“include”的参数。因此,可以使用“todos/?include=comments”、“todos/?include=comments,reviews”调用API。通过这样做,可以只使用基本数据或额外数据获取TODO。。。那不是个问题吗?this.todos将有一个todo列表,但其中一些包含额外数据?或者tap是否也会填充此.todos中现有TODO上缺少的属性?您可以将参数作为函数参数传递,这样会更灵活一些。您可以链接操作符,以便以您可能需要的任何方式映射数据。