刚发现BrowserModule和HttpClientModule没有实现NgModule接口.有人可以解释一下原因吗?
更新:我试图解决的实际问题是提取导入部分以分离我可以根据特定规则创建的数组.所以我的想法是指定这个数组的类型.我不想放在any这里:
const importedModules: any[] = [
BrowserModule,
HttpClientModule,
FormsModule,
SharedModule,
...
];
if (!environment.production) {
importedModules.push(StoreDevtoolsModule.instrument());
}
@NgModule({
//...
imports: importedModules,
//...
})
Run Code Online (Sandbox Code Playgroud) 我有一个客户端具有日期类型属性的对象.当我尝试将对象发送HttpClient.post到服务器时,属性的值将更改为UTC时区.
客户端值为2017年11月26日星期日00:00:00 GMT + 0300(土耳其标准时间),但当它进入服务器时,更改为:25.11.2017 21:00:00
我该怎么控制呢?
这是我的班级.
export interface IBill {
BillID : number;
SubscriptionID:number;
SiteID : number;
SubscriptionName: string;
Amount: number;
Date: Date;
PaymentDeadline: Date;
Virtual: boolean;
Portioned: boolean;
Issuanced: boolean;
FinancialTransactionComment : string;}
Run Code Online (Sandbox Code Playgroud)
我在填写ng-form时创建了一个对象,然后调用Http.post:
let bill = this.formData;
this.http.post(this.configuration.ServerWithApiUrl + url, bill , { headers: headers, withCredentials: true, observe: "response", responseType: 'text' })
.map((response) => {
return this.parseResponse(response);
}).catch(
(err) =>
this.handleError(err));
Run Code Online (Sandbox Code Playgroud) 基本上如标题所述,我需要返回多个可观察值或一个结果。基本上,目标是加载并说出图书馆列表,然后根据该图书馆ID加载书籍。我不想在组件中调用服务,而是希望在页面加载之前先加载所有数据。
import { Injectable } from '@angular/core';
import { Resolve, ActivatedRouteSnapshot } from '@angular/router';
import { UserService } from './../_services/index';
@Injectable()
export class LibraryResolver implements Resolve<any> {
constructor(private _userService: UserService) {}
resolve(route: ActivatedRouteSnapshot) {
return this._userService.getLibraryList();
}
}
Run Code Online (Sandbox Code Playgroud)
如何先加载图书馆列表,然后再加载每个图书馆的图书信息并返回到我的组件?
PS:我的服务通过ID加载了此方法
this.userService.getLibraryBooks(this.library["id"]).subscribe((response) => {
...
Run Code Online (Sandbox Code Playgroud) 如何利用 Angular HTTPClient 的progress事件来显示不一定是文件请求的 Get 请求的百分比进度?
当前 HTTPClient 的progress事件在请求完成后触发。我希望Content-Length在后端工作并确定前端加载的内容百分比。
我正在为网格加载大量行,并且需要在 UI 上显示增量进度。是否可以?
我有一个调用 .NET Core 2.0 后端 api 的 angular 5 应用程序。从 angular 服务调用 API 在所有浏览器(Chrome、Firefox、IE11)中都可以正常工作,但在 EDGE 中不行。在 EDGE,我收到错误
Http failure response for (unknown url): 0 Unknown Error.
Run Code Online (Sandbox Code Playgroud)
这是一个 CROSS ORIGIN CALL 并且 Cors 已在 API 中启用,如下所示。现在我正在启用任何来源
services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins",
builder =>
{
builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
});
});
Run Code Online (Sandbox Code Playgroud)
并在配置方法中
app.UseCors("AllowAllOrigins");
Run Code Online (Sandbox Code Playgroud)
它适用于除 Edge 之外的所有浏览器。感谢你的帮助。
我将Angular项目更新为Angular 6,并且不知道如何进行http获取请求.这就是我在Angular 5中的表现:
get(chessId: string): Observable<string> {
this.loadingPanelService.text = 'Loading...';
this.loadingPanelService.isLoading = true;
const url = `${this.apiPathService.getbaseUrl()}api/chess/${chessId}/rating`;
return this.http.get<string>(url)
.catch((error) => {
console.error('API error: ', error);
this.loadingPanelService.isLoading = false;
this.notificationService.showErrorMessage(error.message);
return Observable.of(null);
})
.share()
.finally(() => {
this.loadingPanelService.isLoading = false;
});
Run Code Online (Sandbox Code Playgroud)
这就是我现在正在做的事情.这是应该如何在Angular 6中完成的吗?
...
return this.http.get<string>(url)
.pipe(
catchError(this.handleError),
share(),
finalize(() =>{this.loadingPanelService.isLoading = false})
);
private handleError(error: HttpErrorResponse) {
console.error('API error: ', error);
this.loadingPanelService.isLoading = false;
this.notificationService.showErrorMessage(error.message);
// return an observable with a user-facing error message
return throwError(
'Something bad happened; …Run Code Online (Sandbox Code Playgroud) 我有以下http.service.ts用于调用/loginAPI的Angular .
login(user: User) {
console.log("logging in");
console.log(JSON.stringify(user));
this.http
.post<any>(this.loginURL, user, httpOptions)
.subscribe(
res => console.log('response : ' + res),
err => console.log('error : ' + err))
}
Run Code Online (Sandbox Code Playgroud)
这是一个非常标准的POST电话,但它总是返回null,我无法理解.这是NetworkGoogle Chrome的标签信息
Chrome的Console信息
我无法理解为什么回应null.即使我没有有效载荷,它至少应该headers可用吗?
我有一个服务器呼叫可能返回HTTP 202。受此SO线程影响,我有以下几点:
this.http.get(url)
.pipe(
map(response => {
if (response.status === 202) {
throw response;
}
return response;
}),
retryWhen(errors => {
return errors.pipe(
delay(1000),
take(3),
concat(response => Observable.throw('Retries exceeded'))
);
}),
catchError(handleError)
);
Run Code Online (Sandbox Code Playgroud)
收到deprecated有关使用的警告concat。我知道新消息concat已经出现,rxjs而没有rxjs/operator。
但是,static concat在这里使用new 运算符的正确方法是什么?
从该站点发现以下内容
import { concat } from 'rxjs/operators';
a$.pipe(concat(b$, c$));
// becomes
import { concat } from 'rxjs';
concat(a$, b$, c$);
Run Code Online (Sandbox Code Playgroud)
我无法确定Observable示例代码中连接了哪些s?
更新1
将代码更改为:
return …Run Code Online (Sandbox Code Playgroud) 我在使文件下载在我的应用程序中工作时遇到了一些麻烦。要下载文件,我正在使用API进行新的调用,以将数据输入到应用程序中。我已经使用Postman测试了此API,并且由于可以通过调用下载文件,因此它似乎可以正常工作。
不幸的是,在将其实现到Angular应用程序时遇到了一些问题。当我调用下面的函数时,由于无法打开,因此我得到的是“损坏”文件。我已经检查了与我的问题有关的其他问题/解决方案,但是在尝试了大多数问题/解决方案后,我没有得到进一步的解决。
我服务中的电话:
DownloadFile (companyId: string, fileId: string, extension: string, fileName: string): Observable<Blob> {
const options = { responseType: 'blob' as 'json' }
return this.http.get<Blob>(this.baseApiUrl + this.baseTag + "?companyId=" + companyId + "&fileId=" + fileId + "&extension=" + extension + "&fileName=" + fileName, options)
}
Run Code Online (Sandbox Code Playgroud)
使用服务调用:
this.data.DownloadFile(this.company.id, selectedItem.id, this.getFileNameWithoutExtension(selectedItem.fileName), this.getExtensionFromFileName(selectedItem.fileName))
.subscribe(resultBlob =>
{
//Success
console.log('start download:', resultBlob);
var blob = new Blob([resultBlob], { type: "application/pdf" } );
saveAs(blob, selectedItem.fileName);
},
error => {
//Error
console.log(error);
});
Run Code Online (Sandbox Code Playgroud)
我没有看到这段代码有什么问题。
在我这里,UserService我有一个用户Observable对象,该对象保存已UserModel登录用户ngOnInit()的身份。为进行测试,我在登录过程中实现了该对象:
this.userService.authenticate('###', '###')
.subscribe(res => console.log('authenticated'));
Run Code Online (Sandbox Code Playgroud)
private userSource = new BehaviorSubject<UserModel>(null);
public user = this.userSource.asObservable();
Run Code Online (Sandbox Code Playgroud)
我的UserModel提供了一个称为authKey的属性,用于API身份验证。
在我ProjectService我想要做的API请求; 为此,UserModel必须存储在中的api密钥。只可能订阅用户属性,但是我读到有关避免在服务内部进行订阅的信息。
题
如何将该订阅与管道/映射连接起来?我的方法是以下代码;但这感觉像是不好的代码。
suggest(term: string): Observable<ProjectModel[]> {
return this.userSrv.user.pipe(
mergeMap((user: UserModel) => {
const options = {params: {'access-token': user.accessToken}};
return this.http.get<ProjectModel[]>(this.conf.url, options).pipe(
map(response => {
// mapping the projects ...
return projects;
})
);
})
);
}
Run Code Online (Sandbox Code Playgroud)