当依赖项中的项目具有相同的名称时,您如何管理冲突?

How do you manage conflicts when items in dependencies have the same name?

这是一个关于 Angular 体系结构以及如何使用它的问题。

我创建了一个非常简单的 Angular 应用程序。它有一个名为 App 的主模块,App 有两个依赖项,Dep1Dep2。两个 Dep 都有一个名为 theConst 的常量。那么当App中使用theConst时,Angular如何决定使用什么?

现在,当我测试应用程序时,Angular 选择 Dep2 的值,这是第二次注入。如果我想在某些地方引用 Dep1 的值而在其他地方引用 Dep2 怎么办?

代码:

<html>
<head>
    <title>Testing</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
    <script src="js/Dependency1.js"></script>
    <script src="js/Dependency2.js"></script>
    <script src="js/app.js"></script>
</head>
<body ng-app="App">
    <div ng-controller="Ctrl">
        <h1>{{greet}}</h1>
        <h5>{{const}}</h5>
    </div>
</body>
</html>

Javascript:

angular.module('Dep1', [])
.constant('theConst', "Number One!");

angular.module('Dep2', [])
.constant('theConst', "Number Two!");


angular.module('App', ['Dep1', 'Dep2'])
.controller('Ctrl', ['$scope', 'theConst', function($scope, theConst){
    $scope.greet = "Howdie!";

    $scope.const = theConst;
}]);

Angular 模块是个奇怪的东西(我不喜欢它们 - 但这是个人意见)。它们只提供抽象分组,没有命名空间,没有 bundling/script 加载。

最后是这样的:你必须自己确保没有 2 个工件具有相同的名称。可能有 Grunt/Gulp/you 构建系统的插件可以帮助您,但我不知道。

Angular 将选择它遇到的最后一个具有给定名称的工件。 (当在多个文件中声明工件时,这会变得很有趣。)