简体   繁体   English

AngularJS通过组合javascript文件来减少HTTP请求

[英]AngularJS reducing HTTP request by combining javascript files

Using angularJS is great. 使用angularJS很棒。 Although i see a problem in that i have the following files: 虽然我看到一个问题,我有以下文件:

  • app.js app.js
  • controllers.js controllers.js
  • filters.js filters.js
  • directives.js directives.js
  • services.js services.js

That along with the actually AngularJS library files and perhaps jQuery is allot of http requests to the server. 这与实际的AngularJS库文件以及jQuery一起分配给服务器的http请求。

Is there a way to combine all these together for a production environment? 有没有办法将所有这些组合在一起用于生产环境? More to the point is there a specific AngularJS recommended way to do this? 更重要的是有一个特定的AngularJS推荐的方法吗?

The Grunt.js project is able to execute tasked commands such as minification and concatenation as I described above. 如上所述,Grunt.js项目能够执行任务命令,例如缩小和连接。 The two grunt.js plugins with the widest support for these tasks are: 对这些任务提供最广泛支持的两个grunt.js插件是:

  • Uglify (minification) Uglify(缩小)
  • Concat (file concatenation) Concat(文件串联)

below is a sample task script for these: 下面是这些的示例任务脚本:

module.exports = function(grunt) {

  grunt.initConfig({
    concat: {
      options: {
        separator: ';'
      },
      dist: {
        src: ['assets/**/*.js'],
        dest: 'assets/js/<%= pkg.name %>.js'
      }
    },
    uglify: {
      options: {
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n'
      },
      dist: {
        files: {
          'assets/js/<%= pkg.name %>.min.js': ['<%= concat.dist.dest %>']
        }
      }
    },
  });

  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-concat');

  grunt.registerTask('default', ['concat', 'uglify']);

};

I dont usually answer my onw questions although i believe that relying on a server to compile, cache and check cached versions is not the way to go. 我通常不回答我的onw问题,虽然我相信依靠服务器来编译,缓存和检查缓存版本是不可取的。 Unnecessary processors and systems to maintain on a production environment. 在生产环境中维护的不必要的处理器和系统。

Combining and minification of script is mostly done on server. 脚本的组合和缩小主要在服务器上完成。 Different technologies\\tools provide different mechanism to support this feature. 不同的技术\\工具提供不同的机制来支持此功能。 I dont think it is a AngularJS concern. 我不认为这是一个AngularJS问题。

The only thing you need to take care if using minification is dependency injection can break in Angular. 如果使用缩小,唯一需要注意的是依赖注入可以在Angular中中断。 So this form of DI would be preferred. 因此,这种形式的DI将是首选。

app.controller('myController',['dependency1',
    function(dependency1) {
    }
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM