这是我的Angular5应用程序中的一个小数据服务.我试图从数组中添加/删除项目(实际上是一个BehaviorSubject).
data.service.ts
private roomArr_source = new BehaviorSubject<any[]>([]);
current_roomArr = this.roomArr_source.asObservable();
addRoomArr(data: any) {
this.roomArr_source.next(this.roomArr_source.getValue().concat([data]));
}
removeRoomArr(data: any) {
this.roomArr_source.forEach((item, index) => {
if(item === data) { this.roomArr_source.splice(index, 1); }
});
}
Run Code Online (Sandbox Code Playgroud)
addRoomArr函数有效,但不适用于removeRommArr.错误是:
error TS2345: Argument of type '(item: any, index: any) => void' is not assignable to parameter of type '(value: any[]) => void'.
error TS2339: Property 'splice' does not exist on type 'BehaviorSubject<any[]>'.
Run Code Online (Sandbox Code Playgroud)
不知何故,我必须将我的BehaviorSubject转换为数组,以便我可以使用.forEach().我怎么能做到这一点?
谢谢
我正在使用带有FOSUserBundle的Angular5和Symfony 3.4.
我有类似的东西:
register(username: string, email: string, first: string, second: string)
{
return this.appHttp.post('register', {username, email, first, second}).subscribe(
data => {
this.appHttp.logRequest(data);
}
);
}
Run Code Online (Sandbox Code Playgroud)
我需要发送给API的是一个格式化的json,如:
{
"email": "email@test.com",
"username" : "userTest",
"plainPassword" : {
"first" : "123",
"second": "123"
}
}
Run Code Online (Sandbox Code Playgroud)
目前,我的json看起来像:
{
"email": "email@test.com",
"username" : "userTest",
"first": "123",
"second": "123",
}
Run Code Online (Sandbox Code Playgroud)
我如何发布所需的json?
谢谢!