我有一个 Angular 应用程序,我试图将预订列表从服务器下载到 Angular 服务。我想让所有组件都能够将预订数组作为可观察的,并能够过滤和限制结果。现在我正在使用BehaviorSubject。这是我的代码。
//reservation.service.ts (partial code)
@Injectable()
export class TripService {
public Trips: Object[] = [];
public TripSubject: Subject<Object[]> = new BehaviorSubject<Object[]>([]);
public trips$: Observable<Object[]> = this.TripSubject.asObservable();
constructor(private apollo: Apollo) {
this.apollo.query({query: ReservationQuery}).subscribe((trips: any) => {
this.Trips = trips.data.allReservations;
this.TripSubject.next(this.Trips)
})
}
}
//partial component
ngOnInit(): void {
this.tripService.trips$.take(3).subscribe(trips => {
this.Trips = trips
})
}
Run Code Online (Sandbox Code Playgroud)
我得到了要填充的预订,但我得到了所有的预订,而不仅仅是通过使用 take(3) 得到了我想要的 3 个。take 操作符不工作有什么原因吗?
是否有从 NGRX 存储的多个切片获取数据的最佳实践?我正在使用 NGRX 实体,我有一个用于公司、用户、供应商和订单的切片,在特定组件中,我需要访问所有 4 个组件。我尝试了几种不同的方法,但它们看起来都很麻烦。
我应该使用来自多个实体的 4 个单独的选择器,还是最好在根级别创建一个选择器并包含详细信息页面所需的所有内容?
目前,我正在使用combineLatest运算符
this.subscription.add(this.store.select(fromUser.getUsers).pipe(
combineLatest([
this.store.select(fromCompanies.selectAll),
this.store.select(fromVendors.selectAll),
this.store.select(fromOrders.getOrders),
]))
Run Code Online (Sandbox Code Playgroud) 我正在尝试从 Excel 中的 VBA 应用程序发送 JSON 对象。下面的代码正确发送请求,但是我不知道如何使用正文中的 JSON 对象发出请求。
Sub Post()
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
URL = "http://localhost:3000/test"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-type", "application/x-www-form- urlencoded"
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.send ("test=6")
End Sub
Run Code Online (Sandbox Code Playgroud)
例如,如果我尝试发送“{test:6, test2: 7}”并在服务器上记录请求的正文,我会得到 { '{parts:6, test: 7}': '' }
我正在尝试获取角度2服务以从HTTP请求中检索数据并将其作为承诺返回.当我在组件中使用该服务时,我从服务传递的数据将返回为undefined.
这是我的服务
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class RecordService {
constructor(private http: Http) {}
getPosts(): Promise<any> {
return this.http
.get('https://jsonplaceholder.typicode.com/posts')
.toPromise()
.then((response: Response) => response.json().data)
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
console.log('ERROR');
return Promise.reject(error.message || error);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的组成部分
import { Component, OnInit } from '@angular/core';
import { RecordService } from './record.service';
import { Router } from '@angular/router';
@Component({
selector: 'record-view',
template: '<h1>This …Run Code Online (Sandbox Code Playgroud) 我正在尝试在AWS Lambda中使用promises并遇到一些麻烦.我正在使用typescript/nodejs; 见下面的代码
export function handler(event: any, context: any, callback: Function){
testFunction().then(data => callback(null, "success from promise"));
callback(null, "success");
}
export function testFunction(){
return new Promise((resolve, reject) => {
setTimeout(() => resolve("data"), 5000);
});
}
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我得到的是"成功回调",而不是"承诺的成功".我在这里做错了吗?
我有一个使用ngrx效果的角度应用程序,正在与之一起加载Firestore数据。我目前收到此警告:
您在需要流的地方提供了“()=>({type})”。您可以提供一个Observable,Promise,Array或Iterable。
这是我的NGRX效果
loadVendors$: Observable<Action> = createEffect(() => this.actions$.pipe(
ofType(fromVendorActions.loadVendors),
switchMap(() => this.store.select(fromRoot.getRouterState)),
tap((ev: any)=> console.log('before', ev)),
switchMap((action:any) => this.vendorService.getVendors(action.state.params.vendorGroupId)),
tap((ev: any)=> console.log('after', ev)),
map((vendors: any) => fromVendorActions.loadVendorsSuccess({vendors: vendors})),
catchError(() => fromVendorActions.loadVendorsFail)
))Run Code Online (Sandbox Code Playgroud)
我正在从操作切换,获取我所在页面的参数,然后从服务切换到可观察对象,以检索一些可观察对象的数据。
使用tap()检查流,我似乎得到了期望的结果。警告出现在控制台中,在我记录流的两个(2)抽头之间。数据已正确加载到存储中,但警告困扰着我。
我正在使用 ngrx 并且有一个场景,我需要同时调度 2 个动作。我的状态具有用于更新和更新的属性,如下所示。
//from reducer
const defaultCardState: CardState = {
ids: [],
entities: {},
loaded: false,
loading: false,
adding: false,
added: false,
updating: false,
updated: false,
deleting: false,
deleted: false
};
Run Code Online (Sandbox Code Playgroud)
这些是我从我的组件分派的动作
this.store.dispatch(fromCard.updateCard({id: id1, changes: {name: name1}}))
this.store.dispatch(fromCard.updateCard({id: id2, changes: {name: name2}}))
Run Code Online (Sandbox Code Playgroud)
下面是我的动作,减速器和效果
//Update Card Actions
export const updateCard = createAction('[Cards] Update Card', props<{id: string, changes: any}>())
export const updateCardSuccess = createAction('[Cards] Update Card Success', props<{changes: any}>());
export const updateCardFail = createAction('[Cards] Update Card Fail')
//Reducer
on(fromCards.updateCard, (state) …Run Code Online (Sandbox Code Playgroud) angular ×5
rxjs ×4
ngrx ×3
typescript ×3
aws-lambda ×1
excel ×1
javascript ×1
json ×1
node.js ×1
redux ×1
vba ×1