可以将两个自定义方法添加到 ngx-restangular 资源吗?

Can two custom methods be added to ngx-restangular resource?

我正在使用多种方法配置 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 方法?

有可能。我通过为每个要添加的方法调用 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.

的一部分添加时,也有必要为每个添加身份验证 header