Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 在服务中使用Delete http命令_Angular - Fatal编程技术网

Angular 在服务中使用Delete http命令

Angular 在服务中使用Delete http命令,angular,Angular,我想知道一些有关删除功能的信息。请参阅以下代码:- // Method in service delete(Id: string): Observable<any> { this.Url = 'http://locahost:8080/...'; return this.http.delete(this.Url) .map((response:Response) => response.json())

我想知道一些有关删除功能的信息。请参阅以下代码:-

// Method in service
 delete(Id: string): Observable<any> {
        this.Url = 'http://locahost:8080/...';
        return this.http.delete(this.Url)
            .map((response:Response) => response.json())
            .catch(this.handleError);
    }

    // Method in component
    deleteFromComp(Id: string): void {
    this.subscriptions.push(this.MyService.delete(Id).subscribe(
      data => {
      },
      error => this.errorMessage = <any>error, () => { }
    ));

    this.CallServciceToGetDataFromServer(this.PageRequestModel);
  }
//服务中的方法
delete(Id:string):可观察{
这个.Url=http://locahost:8080/...';
返回this.http.delete(this.Url)
.map((response:response)=>response.json())
.接住(这个.把手错误);
}
//组件中的方法
deleteFromComp(Id:string):无效{
this.subscriptions.push(this.MyService.delete(Id).subscription(
数据=>{
},
error=>this.errorMessage=error,()=>{}
));
this.CallServciceToGetDataFromServer(this.PageRequestModel);
}
delete方法调用服务器端代码来删除记录,并且不返回任何内容

我有以下两个问题:-

  • 如果没有任何回报,为什么需要订阅?是不是只有200个信息码
  • 以下修改的调用不起作用:-

    // Method in component
            deleteFromComp(Id: string): void {
            this.subscriptions.push(this.MyService.delete(Id).subscribe(
              data => {
                this.CallServciceToGetDataFromServer(this.PageRequestModel);
              },
              error => this.errorMessage = <any>error, () => { }
            ));
    
          }
    
    //组件中的方法
    deleteFromComp(Id:string):无效{
    this.subscriptions.push(this.MyService.delete(Id).subscription(
    数据=>{
    this.CallServciceToGetDataFromServer(this.PageRequestModel);
    },
    error=>this.errorMessage=error,()=>{}
    ));
    }
    

  • 如果您不订阅
    可观察对象
    ,它们将不会执行

    由于使用
    httpClient
    所有
    HTTP请求
    返回
    可观察对象
    ,您应该
    订阅它们

    订阅可观测数据:

    记住,可观察对象是懒惰的。如果你不订阅,什么都不是 就要发生了。当你订阅一个 observer,对subscribe()的每次调用都将触发它自己的独立 对给定观察者的执行。订阅呼叫不共享 在同一可观察对象的多个订户之间

    下面是一个示例片段:

    import { Observable } from "rxjs/Observable"
    // create observable
    const simpleObservable = new Observable((observer) => {
        // observable execution
        observer.next("bla bla bla")
        observer.complete()
    })
    // subscribe to the observable
    simpleObservable.subscribe()
    // dispose the observable
    simpleObservable.unsubscribe()
    
    在本例中,直到您订阅,即直到您调用无法执行的
    simpleObservable.subscribe()

    PS:


    使用
    订阅
    的主要用途之一是检查
    删除状态
    ,例如,如果
    后端
    抛出一个
    错误
    ,该怎么办,然后,您可以使用
    error
    回调中的
    res.status
    response.success
    error
    检查状态{
    deleteFromComp(Id: string): void {
            this.subscriptions.push(this.MyService.delete(Id).subscribe(
              () => {
                this.CallServciceToGetDataFromServer(this.PageRequestModel);
              },
              error => this.errorMessage = <any>error, () => { }
            ));
    
          }
    
    this.subscriptions.push(this.MyService.delete(Id).subscription( () => { this.CallServciceToGetDataFromServer(this.PageRequestModel); }, error=>this.errorMessage=error,()=>{} )); }
    如果响应中没有内容,则应为204无内容,而不是200 OK。你需要订阅,因为所有的http方法都是冷可观察的,只有在有订阅者的情况下才会发出请求。我正要说冷可观察的,请在这里阅读更多关于可观察的内容:感谢链接。从api中,它们可能不会返回任何内容,但仍然是
    delete
    is
    http方法
    ,它返回一个observableI得到了我的答案,代码可以修改如下:-deleteFromComp(Id:string):void{this.subscriptions.push(this.MyService.delete(Id.subscribe());this.CallServciceToGetDataFromServer(this.PageRequestModel);}是的,所以你应该订阅它。这就是解决办法。这是你的愿望,你想要任何数据或不是从我已经订阅了肯定,但我不确定,什么是正确的方式,因为我可以看到返回类型,如果观察和删除方法不返回任何东西从服务器,所以这是有点混乱,但我有更好的信息在这个场景。非常感谢大家。检查删除状态非常重要,例如,如果后端抛出错误,那么您可以使用
    res.status
    response.success
    error
    回调中的error来检查状态