在Angular中,要接受由后端发送的cookie,可以使用HttpClient的withCredentials选项。
示例代码:
import { HttpClient } from '@angular/common/http';
constructor(private http: HttpClient) {}
this.http.get('http://example.com', { withCredentials: true }) .subscribe(response => { // 处理响应 });
在上面的代码中,通过withCredentials选项将cookie传递到服务器端。在这个示例中,替换URL为适当的后端URL即可。
请注意,withCredentials选项默认是false,因此需要将其设置为true以启用cookie传递。