我正在使用多个方法配置ngx-restangular资源,但只有第一种方法被识别为函数。通过以下配置,可以识别feature
功能,但不能识别nearest
功能。
restangular.withConfig(
(configurer) => {
/* Add Auth header to each request. */
configurer.addFullRequestInterceptor(
(element, operation, path, url, headers, params) => {
return {
headers: Object.assign({}, headers, {Authorization: `Bearer ${creds.getBearerToken()}`})
};
}
);
/* For a given locationId, provide a feature set (geometry) for the location. */
configurer.addElementTransformer('location', false,
(resource) => {
resource.addRestangularMethod(
'feature',
'get',
'map'
);
return resource;
}
);
/* Resource providing Nearest Location instances suitable for edit. */
configurer.addElementTransformer('location', true,
(resource) => {
resource.addRestangularMethod(
'nearest',
'get',
'nearest'
);
return resource;
}
);
}
);
return restangular.service('location');
是否可以添加多个Restangular方法?
答案 0 :(得分:0)
有可能。我通过为每个要添加的方法调用withConfig
来实现此目的:
restangular.withConfig(
(configurer) => {
/* Add Auth header to each request. */
configurer.addFullRequestInterceptor(
(element, operation, path, url, headers, params) => {
return {
headers: Object.assign({}, headers, {Authorization: `Bearer ${creds.getBearerToken()}`})
};
}
);
/* For a given locationId, provide a feature set (geometry) for the location. */
configurer.addElementTransformer('location', false,
(resource) => {
resource.addRestangularMethod(
'feature',
'get',
'map'
);
return resource;
}
);
}
);
restangular.withConfig(
(configurer) => {
/* Add Auth header to each request. */
configurer.addFullRequestInterceptor(
(element, operation, path, url, headers, params) => {
return {
headers: Object.assign({}, headers, {Authorization: `Bearer ${creds.getBearerToken()}`})
};
}
);
/* Resource providing Nearest Location instances suitable for edit. */
configurer.addElementTransformer('location', true,
(resource) => {
resource.addRestangularMethod(
'nearest',
'get',
'nearest'
);
return resource;
}
);
}
);
return restangular.service('location');
当作为withConfig
的一部分添加时,还需要为每个标题添加auth标头。