小编Ada*_*ski的帖子

Angular 6 ngrx,如何在状态对象中向数组中添加新项?

我有一个简单的情况,我有像CreatUser,CreateSuccess,CreateFail这样的动作.我应该如何向数组中添加新对象以及何时Create调度操作CreateSuccess?那我该怎么办?

export function reducer(state = init, action: Actions): State {
switch (action.type) {
    case ActionsTypes.CREATE:
        return {
            ...state,
            inProgress: true
        };
    case ActionsTypes.CREATE_SUCCESS:
        return {
            ...state,
            users: state.users.push(action.payload),
            inProgress: false
        };
    case ActionsTypes.CREATE_FAIL:
        return {
            ...state,
            error: action.payload,
            inProgress: false
        };
    default:
        return state;
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我尝试使用push方法添加新用户,但这不是一个好的解决方案.我该怎么办?

ngrx angular ngrx-store

9
推荐指数
4
解决办法
7466
查看次数

Angular 6 NGRX 分页,如何定义高级分页的操作

我有一个带有 NgRx 的 Angular 6 应用程序,但我在如何正确定义操作和状态方面遇到了问题。

行动:

export class LoadUsers implements Action {
    readonly type = UserActionTypes.LoadUsers;
    constructor(public payload: { pagination: Pagination }) {
    }
}
export class LoadUsersSuccess implements Action {
    readonly type = UserActionTypes.LoadUsersSuccess;
    constructor(public payload: { users: User[], pagination: Pagination}) {
    }
}
Run Code Online (Sandbox Code Playgroud)

分页界面:

export interface Pagination {
    pageSize?: number;
    pageIndex?: number;
    orderBy?: string;
    dir?: string;
    filter?: string;
}
Run Code Online (Sandbox Code Playgroud)

状态:

export interface State {
    pagination: Pagination;
    users: User;
}
Run Code Online (Sandbox Code Playgroud)

减速器:

export function reducer() {
switch (action.type) { …
Run Code Online (Sandbox Code Playgroud)

pagination ngrx angular

6
推荐指数
1
解决办法
4208
查看次数

Binance API和angular 4 httpClient

我有一个关于cryptomarket Binance的问题.他们有公共api我虽然我可以使用角度来创建交易应用程序.

但我有一些麻烦.在chrome中使用该链接我得到json结果. https://api.binance.com/api/v1/exchangeInfo

但使用angular 4 httpClient:

this.http.get('https://api.binance.com/api/v1/exchangeInfo').subscribe(res => console.log(res));
Run Code Online (Sandbox Code Playgroud)

我有错误:跨源请求被阻止:同源策略不允许在api.binance.com/api/v1/exchangeInfo上读取远程资源.(原因:缺少CORS标题'Access-Control-Allow-Origin')

它不起作用.我不明白,为什么我不能在角应用程序中使用该API?https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md

我该怎么办?我应该像这样设置标题:

getMarkets() {
    const headers = new HttpHeaders();
    headers.set('Content-Type', 'application/json');
    headers.set('Accept', 'application/json');
    headers.set('Access-Control-Allow-Headers', 'Content-Type');
    headers.set('Access-Control-Allow-Origin', '*');

    const path = 'https://api.binance.com/api/v1/exchangeInfo';
    return this.http.get(path, {headers: headers});
}
Run Code Online (Sandbox Code Playgroud)

提前致谢

javascript http cors typescript angular

5
推荐指数
2
解决办法
3621
查看次数

带有方法 GET 的 Angular HttpClient 参数不起作用

我想问你关于 get 方法的参数。我有这样的事情:

    path = 'https://example.com/api';
    const params = new HttpParams();
    params.append('http', 'angular');
    return this.http.get(path, {params: params});
Run Code Online (Sandbox Code Playgroud)

我以为我会有这样的网址:

www.example.com/api?http=angular

但实际上,当我在 chrome 的网络选项卡中检查时,请求 url 是

www.example.com/api

当我想要路径时应该怎么做: www.example.com/api?http=angular 是否可以在没有 chrome 的情况下检查使用方法的请求 url?我的意思是在使用该方法的服务中?

http typescript angular-http angular

4
推荐指数
1
解决办法
2983
查看次数

RxJS switchMap 并将响应传递给另一个操作员

假设我有这样的代码:

            this.apiService.getUrlForPhoto('photo1')
            .switchMap((url) => {
                return this.apiService.uploadPhotoToProvidedUrl(url);
            })
            .switchMap((response) => {
                return this.apiService.confirmPhotoUpload(confirmationToken);
            })
            .subscribe((response) => {
                if (response.confirmed) {
                    console.log('Success');
                }
            });
Run Code Online (Sandbox Code Playgroud)

首先是http get,它返回我可以发布新照片的url,其次是http放在我必须上传照片的地方,第三个是http post,我必须确认照片已上传到该url。

我的问题是可以将 url 传递给第二个 switchMap 吗?在第二个 switchMap 中,我收到了来自 uploadPhotoToProviderdUrl 方法的响应,但如何从以前的方法获得响应?

rxjs angular switchmap

2
推荐指数
1
解决办法
2261
查看次数