angular-ngrx-data - 带有@NGRX/Data 的自定义端点
问题描述
我需要使用@NGRX/Data 修改我的端点。我编写了一个在文档中描述的数据服务,它确实改变了端点,问题是数据没有加载到存储中。如何使用此自定义数据服务并在收到数据后将数据加载到存储中?
@Injectable()
export class OfficeCustomService extends DefaultDataService<Office> {
userId: number;
territoryId: number;
constructor(http: HttpClient ) {
const url = new DefaultHttpUrlGenerator(new DefaultPluralizer([appPluralNames]));
url.registerHttpResourceUrls({Label: {entityResourceUrl: 'office', collectionResourceUrl: 'offices'}});
super('Office', http, url, defaultDataServiceConfig);
}
protected execute(method: HttpMethods, url: string, data?: any, options?: any): Observable<any> {
const regex = /offices/gi;
if (method === 'GET' && url.search(regex) > 0) {
url = url + 'userId/' + this.userId + '/territoryId/' + this.territoryId;
}
return super.execute(method, url, data, options);
}
getAllByUserAndTerritory(userId: number, territoryId: number): Observable<Office[]> {
this.userId = userId;
this.territoryId = territoryId;
return super.getAll();
}
}
为什么 super.getAll() 函数不将数据加载到存储中?
解决方案
我无法弄清楚为什么 super.getAll() 不起作用,但我找到了解决方法。我没有将用户 ID 和区域 ID 传递给自定义函数,而是将它们从数据服务的存储中拉出来。不是我想做的,但它确实有效。
@Injectable()
export class OfficeCustomService extends DefaultDataService<Office> {
userData: User;
constructor(
http: HttpClient,
store: Store ) {
const url = new DefaultHttpUrlGenerator(new DefaultPluralizer([appPluralNames]));
url.registerHttpResourceUrls({Label: {entityResourceUrl: 'office', collectionResourceUrl: 'offices'}});
super('Office', http, url, defaultDataServiceConfig);
store.select(fromState.getUser).subscribe((user: User) => {
this.userData = user;
});
}
protected execute(method: HttpMethods, url: string, data?: any, options?: any): Observable<any> {
const regex = /offices/gi;
if (method === 'GET' && url.search(regex) > 0) {
url = url + 'userId/' + this.userData.id + '/territoryId/' + this.userData.territoryId;
}
return super.execute(method, url, data, options);
}
}
推荐阅读
- java - 我有这个例外:未解决的要求:导入包:com.liferay.portal.instance.lifecycle;版本="[2.0.0,3.0.0)"
- java - 如何使用 javafx 或其他库有效地绘制许多数据点?
- javascript - 为什么 this.setState 会“不是函数”?
- javascript - 如何将重定向网址嵌入网站?(在 React Native 中)
- mysql - 如何在数据库中进行计算属性
- javascript - 使用 Passport.js 成功登录后如何刷新用户的用户名?
- php - 修复电子邮件模板上的 MIME_HTML_ONLY
- java - Parse(byte[]) 不能应用于 java.lang.string
- python - 生成包含模态值的新列
- javascript - 我需要将下一行放入带有 java 值的弹出 javascript 字符串中