简体   繁体   English

在对AngularJS应用进行模块化之后,如何将整个项目包含在index.html中,而不必键入很多<script> tags

[英]After modularizing an AngularJS app, how do you include the entire project in the index.html without having to type a ton of <script> tags

I've been learning Angular for awhile now and am looking into the best ways to modularize the application. 我已经学习Angular已有一段时间了,并且正在研究模块化应用程序的最佳方法。 I think my understanding of that is going pretty well, but I've looked around for awhile and can't seem to get a good answer as to how all of these .js files get included inside of the index.html without just manually typing out a bunch of tags. 我认为我对此的理解很好,但是我已经浏览了一段时间,而且似乎无法获得关于所有这些.js文件如何包含在index.html中的好的答案,而不仅仅是手动输入一堆标签。 I've been looking into Grunt/Gulp and get how those are combining the entire app into one .js file, but for development I'm guessing you don't want to have to re-run grunt or gulp every time you want to update the app. 我一直在研究Grunt / Gulp,并了解它们是如何将整个应用程序组合到一个.js文件中的,但是为了进行开发,我想您不想每次都重新运行grunt或gulp。更新应用程序。

There are many different options: gulp, grunt, or webpack seem to be the most popular. 有许多不同的选项: gulp ,grunt或webpack似乎是最受欢迎的。 I tend to use webpack the most these days. 这些天我倾向于使用webpack。

A good setup will typically run a local node server, which will refresh the browser automatically every time you make a change to a file. 一个好的设置通常会运行一个本地节点服务器,该服务器将在您每次更改文件时自动刷新浏览器。

There are many yeoman generators that will set this all up for you, or you can find simple examples on github. 有许多yeoman生成器将为您完成所有这些工作,或者您可以在github上找到简单的示例。

The basic idea is to 基本思想是

  1. concatenate all your js files (in proper order so the module definitions go before controllers/services) 连接所有js文件(以正确的顺序排列,以便模块定义先于控制器/服务)
  2. minify if for production 最小化是否用于生产
  3. copy to a fixed path 复制到固定路径
  4. include this single js file in your html 在您的html中包含此单个js文件
  5. during development have your grunt/gulp script watch for changes in js files and re-run the above steps so you don't have to run the grunt/gulp task manually. 在开发过程中,让您的grunt / gulp脚本监视js文件中的更改,然后重新运行上述步骤,因此您不必手动运行grunt / gulp任务。

Now if you are using gulp here is how you would typically handle above steps 现在,如果您正在使用gulp,则通常按照以下步骤操作

gulp.task('process-js', function () {
  return gulp.src(jsFiles, {base: './'})
    .pipe(gulpif(isProd, concat('all.min.js'), concat('all.js')))
    .pipe(gulpif(isProd, uglify({mangle: true, preserveComments:    'some'})))
    .pipe(gulp.dest(deployFolder + '/static/js'))
});

where jsFiles is an array of files/folders that contain your angular app js files 其中jsFiles是包含您的角度应用js文件的文件/文件夹的数组

var jsFiles = [
'!static/js/**/*.js',
'!static/js/plugin/**/*.*',
'app/index/**/*module.js',
'app/index/**/*config.js',
'app/index/**/*ctrl.js'
];

Note: use ! 注意:使用! prefix to exclude certain files/folders from processing. 前缀以排除某些文件/文件夹。

isProd is just a flag that indicates whether you are preparing for production or development. isProd只是一个标志,指示您是准备进行生产还是开发。

During development I also use BrowserSync to watch any changes to my js files and re-run the gulp task to prepare all.js . 在开发期间,我还使用BrowserSync监视对js文件的任何更改,然后重新运行gulp任务以准备all.js It also refreshes the page in browser automatically. 它还会自动刷新浏览器中的页面。

gulp.task('run', function () {
browserSync({
    server: {
        baseDir: deployFolder
    },
    open: true,
    browser: ['google chrome'],
    files: deployFolder + '/**/*',
    watchOptions: {
        debounceDelay: 2000
    }
});
gulp.watch(jsFiles, ['process-js']);

});
gulp.task('default', function () {
  runSequence(
    'clean',
    'run'
  );
});

Gulp/Grunt to concat all your angular files . Gulp/Grunt可以concat所有的angular files

Create 2 tasks : 创建2个任务:

  • a dev build task dev build task
    • concat to one file BUT don't uglify . 连接到一个文件但don't uglify
  • a distribution/production task which is the same as dev one but this one uglify the concatenated file . 一项与开发人员相同的distribution/production task ,但这使concatenated file uglify了。

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

相关问题 如何通过自动将NPM“依赖”包含到index.html中<script> tags in VUE JS WEBPACK project? - How to automatically include NPM “dependencies” into index.html through <script> tags in VUE JS WEBPACK project? AngularJS App:如何将.js文件包含到index.html中 - AngularJS App: How to include .js files into index.html AngularJS:将菜单作为部分包含在index.html中 - AngularJS: Include menu as partial in index.html 如何在脚本标签的index.html中使用Bowser Javascript库? - How to use a Bowser Javascript Library in index.html in script tags? 如何使用angularJS将脚本动态添加到index.html? - How to dynamically add the script to index.html using angularJS? WebPack 反复将冗余脚本标签注入 index.html 加载 - WebPack repeatedly injects redundant script tags into index.html on load 如何使 Vue 从 assets 文件夹中导入文件,而不是 index.html 内的硬编码脚本标签 - How to make Vue import files from assets folder instead of hardcoded script tags inside of index.html ngInclude 不从 index.html 加载脚本 - AngularJS - ngInclude not loading script from index.html - AngularJS 没有Index.html设计的登录页面 - ngRoute(AngularJS) - Login Page without Index.html Design - ngRoute (AngularJS) Angular 2 - 如何使关键字和描述的index.html文件标题和元标记动态化 - Angular 2 - how do I make my index.html files title and meta tags for keywords and description dynamic
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM