我有一个简单的情况,我有像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 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) 我有一个关于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)
提前致谢
我想问你关于 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?我的意思是在使用该方法的服务中?
假设我有这样的代码:
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 方法的响应,但如何从以前的方法获得响应?
angular ×5
http ×2
ngrx ×2
typescript ×2
angular-http ×1
cors ×1
javascript ×1
ngrx-store ×1
pagination ×1
rxjs ×1
switchmap ×1