refactor large AngularJS module config into separate files

前端 未结 3 1529
傲寒
傲寒 2020-12-15 19:53

Problem: Large config()

The config of my AngularJS app is growing quite large. How would you refactor the following into separate files?



        
3条回答
  •  不知归路
    2020-12-15 20:36

    I would look at browserify

    It allows you to use markup to require modules in your javascript.

    This would allow you to break up your config into multiple files while keeping them together (see example).

    browserify compiles your javascript into a single bundle.js by recursively tracing the requires from your main javascript file.

    You then only need to include on your index.html

    Short Example

    Javascript

    'use strict';
    
    require('angular/angular');
    require('angular-ui/ui-router');
    
    var app = angular.module('vw', ['ui.router', 'myApp.feature1', 'myApp.feature2'])
                     .config(require('./config/route'))
                     .config(require('./config/httpProvider'));
    
    angular.bootstrap(document, ['myApp']);
    

    File Structure

    • myApp
      • config
        • route.js
        • httpProvider.js
      • myApp.js
      • index.html

提交回复
热议问题