前端中的Web API资源使用情况

雪熊

我已经遇到过几次这个问题。我更改资源的结果或参数,然后搜索它的使用位置并进行更改。但是我经常错过应用程序的一些晦涩之处。

找到一个角度应用程序中使用API​​资源(端点)的所有位置的好技术是什么?

它并不总是像搜索“ api / foo / bar / baz”那样简单。URI可以由多个变量串联而成。

谢尔盖·科洛迪(Sergey Kolodiy)

我的方法是使用自定义Angular服务,并在其中存储所有端点信息以及其他特定于应用程序的设置:

angular.module("appModule").value("appSettings", {
    title: "My application",
    version: "0.0.0.1",
    webApiHost: "http://localhost:33000/",
    customersEndpoint: "api/customers",
});

这样,您可以通过appSettings.customersEndpoint在代码中查找字符串来找到所有引用这是客户示例:

(function (angular) {
    var customersFactory = function ($http, appSettings) {
        var factory = {};

        factory.getAll = function () {
            return $http.get(appSettings.webApiHost + appSettings.customersEndpoint);
        }        

        return factory;
    };

    customersFactory.$inject = ["$http", "appSettings"];
    angular.module("appModule").factory("customersFactory", customersFactory);
}(angular));

但是,没有什么可以阻止您(或其他人)例如通过使用字符串连接来忽略该技术并混淆端点。因此,这种事情没有灵丹妙药-请小心!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章