concatenate javascript in anonymous function with gulp

旧城冷巷雨未停 提交于 2019-12-11 13:10:54

问题


I use gulp to concat my scripts into one file. Before concatenation I've split up my .js files into seperate files.

So I've got app.js where I declare my angular app and some other stuff. But I've also got route.js where I declare my routing. However, the declaration of my angular app in app.js is wrapped inside an anonymous function, so when it concatenates app.js and route.js, route.js falls outside the scope of the declaration of my Angular app.

Is there some way to inject a script into the scope with Gulp in stead of concatenate it all together? Or is there some other approach I'm missing, apart from didging JSLint and the anonymous function?


回答1:


If you're not using CommonJS or AMD modules and just concatenating scripts then just make sure each concatenated script gets the module it needs in it's own scope.

(function() {

    // Declare app
    var app = angular.module('Application', ['ngRoute']);

    // Do stuff with variable app in scope.

})();

Now in your routes.js

(function() {

    // Grab the already created module for use
    // in this scope.
    var app = angular.module('Application');

    app.config([
        '$locationProvider', 
        '$routeProvider',
        function($locationProvider, $routeProvider) {

            $locationProvider.html5Mode = true;

            $routeProvider.when('/', {
                controller : 'MyCtrl',
                templateUrl : 'index.html'
            });
        }
    ]);
})() 


来源:https://stackoverflow.com/questions/28320122/concatenate-javascript-in-anonymous-function-with-gulp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!