有人可以解释一下lambda/胖箭头角度函数调用的语法和含义吗?



有人可以为我分解一下并解释语法和含义吗?我知道put$具有返回可观察量的含义,因此它调用公司服务中的 put 并且它有两个参数,我知道订阅是什么意思,但我需要一些帮助从那里理解它。

如果你有一个好教程的链接,那就太好了。

this.companyService.put$(this.currentId, this.appMenu.currentObject)
.subscribe(selectedCompany => {this.appMenu.currentObject = selectedCompany});

这是服务:

如果您还可以解释"put"调用的语法,那也会有所帮助。

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, BehaviorSubject } from 'rxjs';
import { Company } from '../models/company.model';
@Injectable({
providedIn: 'root'
})
export class CompanyService {
private url: string;
constructor(private http: HttpClient) {
this.url = "http://localhost:8080/niche/company";
}
getOne$ = (companyId: number): Observable<Company> => this.http.get<Company>(`${this.url}/${companyId}`);
get$ = (): Observable<Company[]> => this.http.get<Company[]>(this.url);
post$ = (company: Company): Observable<Company> => this.http.post<Company>(this.url, { company });
patch$ = (companyId: number, company: Company): Observable<Company> => this.http.patch<Company>(`${this.url}/${companyId}`, { company });
put$ = (companyId: number, company: Company): Observable<Company> => this.http.put<Company>(`${this.url}/${companyId}`, company );
delete$ = (companyId: number): Observable<Company> => this.http.delete<Company>(`${this.url}/${companyId}`);
}

以下行表示:

this.companyService
.put$(this.currentId, this.appMenu.currentObject)
.subscribe(selectedCompany => {
this.appMenu.currentObject = selectedCompany
});

如果你喜欢一个比喻:

银行里有一首诗(database(,你的文案里有一首诗的副本(client application, in your case Angular application(。

但是,您想编辑文案中的一些诗行和银行中的诗

。然后你打电话给一个银行账户官员(this.companyService(把你的行(.put(放到现有的诗歌(this.currentId, this.appMenu.currentObject(,然后你正在等待一个银行账户官员(.subscribe(,直到她/他在银行更新诗歌。

更新:

正如Angular doc所说:

@angular/common/http 中的 HttpClient 提供了一个简化的客户端 HTTP API for Angular 应用程序,它依赖于 XMLHttpRequest 浏览器公开的界面。HttpClient 的其他好处 包括可测试性功能、类型化请求和响应对象, 请求和响应拦截、可观察 API 和简化 错误处理。

正如MDN所说:

XMLHttpRequestXHR是一个 JavaScript API,用于客户端和服务器之间的通信。使用 XMLHttpRequest (XHR( 对象与服务器交互。您可以从 无需执行整页刷新的 URL。这将启用网页 只更新页面的一部分,而不会中断用户的身份 行为。XMLHttpRequest 在 AJAX 编程中被大量使用。

与它的名字相反,XHR可以用来接收JSON,HTML和纯文本以及XML。

以及使用的示例:

// 1. Here we are creating a new XMLHttpRequest object
let xhr = new XMLHttpRequest();
// 2. Then we set its configuration: request of GET type for the URL /somePage/anotherPage
xhr.open('GET', '/somePage/anotherPage');
// 3. We are sending the request over the network
xhr.send();
// 4. Then this rows will be called after the response is received from the 
// server to the client
xhr.onload = function () {
if (xhr.status != 200) { // see what HTTP status of the response is come
alert(`Error ${xhr.status}: ${xhr.statusText}`); // e.g. 404: Not Found
} else { // show the result
alert(`Great, got ${xhr.response.length} bytes`); // responseText is the server
}
};
// Get progress from XMLHttpRequest
xhr.onprogress = function (event) {
if (event.lengthComputable) {
alert(`We are received ${event.loaded} of ${event.total} bytes`);
} else {
alert(`We are received ${event.loaded} bytes`); // no Content-Length
}
};
xhr.onerror = function () {
alert("Request failed");
};

所以subscribeHTTPClient的方法就是onloadXMLHttpRequest的方法。

最新更新