AngularJS 中的模块和命名空间/名称冲突 [英] Modules and namespace / name collision in AngularJS

查看:39
本文介绍了AngularJS 中的模块和命名空间/名称冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下 jfiddle http://jsfiddle.net/bchapman26/9uUBU/29/

Consider the following jfiddle http://jsfiddle.net/bchapman26/9uUBU/29/

//angular.js example for factory vs service
var app = angular.module('myApp', ['module1', 'module2']);

var service1module = angular.module('module1', []);

service1module.factory('myService', function() {
    return {
        sayHello: function(text) {
            return "Service1 says \"Hello " + text + "\"";
        },
        sayGoodbye: function(text) {
            return "Service1 says \"Goodbye " + text + "\"";
        }
    };
});

var service2module = angular.module('module2', []);

service2module.factory('myService', function() {
    return {
        sayHello: function(text) {
            return "Service2 says \"Hello " + text + "\"";
        },
        sayGoodbye: function(text) {
            return "Service2 says \"Goodbye " + text + "\"";
        }
    };
});

function HelloCtrl($scope, myService) {
    $scope.fromService1 = myService.sayHello("World");
}

function GoodbyeCtrl($scope, myService) {
    $scope.fromService2 = myService.sayGoodbye("World");
}​

我有 2 个模块(module1 和 module2).module1 和 module2 都定义了一个名为 myService 的服务.当两个模块都导入到 myApp 中时,这似乎会在 Angular 中的 myService 上创建名称冲突.看来 AngularJs 只是使用了第二个服务定义,而没有警告您可能出现的问题.

I have 2 modules (module1 and module2). Both module1 and module2 define a service called myService. This appears to create a name clash on myService within Angular when both modules are imported into myApp. It appears AngularJs just uses the second service definition without warning you of the possible issue.

非常大的项目(或一般只重用模块)会存在名称冲突的风险,这可能难以调试.

Very large projects (or just reusing modules in general) would have a risk of names clashing, which could be difficult to debug.

有没有办法用模块名称作为名称前缀,以免发生名称冲突?

Is there a way to prefix names with the module name so that name clashes don't happen?

推荐答案

截至今天,AngularJS 模块不提供任何类型的命名空间来防止不同模块中的对象之间发生冲突.原因是 AngularJS 应用程序有一个注入器,它保存所有对象的名称,而不考虑模块名称.

As of today, AngularJS modules do not provide any sort of namespacing that would prevent collisions between objects in different modules. The reason is that an AngularJS app has a single injector that holds names for all objects without respect to module names.

AngularJS 开发者指南说:

为了管理依赖创建的责任,每个 Angular应用程序有一个注入器.注入器是一个服务定位器,它是负责构建和查找依赖项.

To manage the responsibility of dependency creation, each Angular application has an injector. The injector is a service locator that is responsible for construction and lookup of dependencies.

正如您所提到的,将模块注入主/应用程序模块时可能会导致令人讨厌的错误.当碰撞发生时,它们会保持沉默,获胜者由最后注入的模块决定.

As you've mentioned, nasty bugs can result when injecting modules into your main/app module. When collisions happen they are silent and the winner is determined by whichever was the last module injected.

所以不,没有内置的方法可以避免这些冲突.也许这会在未来发生.对于更可能出现此问题的大型应用程序,命名约定是最好的工具是正确的.考虑属于模块或功能区的对象是否可能使用短前缀.

So no, there's not a built in way of avoiding these collisions. Maybe this will happen in the future. For large apps where this problem becomes more likely, you're right that naming conventions are your best tool. Consider whether objects belonging to a module or function area might use a short prefix.

这篇关于AngularJS 中的模块和命名空间/名称冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆