【问题标题】:Best practices to inject custom module in meanjs在 meanjs 中注入自定义模块的最佳实践
【发布时间】:2015-02-19 19:58:06
【问题描述】:

据我了解,没有经典的模块注册,我们可以在其中注入我们的依赖项,例如:

var myModule = angular.module('myModule', [otherModule]);

但是在目录的根目录下有文件module-name.client.module.js

'use strict';
// Use Applicaion configuration module to register a new module
ApplicationConfiguration.registerModule('module-name');

我可以在这里注入我的模块,比如*.registerModule('module-name', [someModule]);,还是应该在angular.module('articles').config(...) 中注入?

但是在config我只能注入providers,没有factories

【问题讨论】:

    标签: angularjs meanjs


    【解决方案1】:

    就我使用的角度而言,加载自定义模块和外部库的最佳实践 就是把angularjs和requirejs结合起来。

    分三步完成。

    首先,在您的主 html 文件中加载一个构建系统基础的 js 文件(require.config): 这里有所有允许的参数:https://github.com/jrburke/r.js/blob/master/build/example.build.js

    示例: 在您的 html 文件中:

    <script src="path/to/require.js" data-main="path/to/yourBuildFile"></script>
    

    在您的BuildFile 文件中:

    require.config({
        // you can define suitable names for all your external js library
        paths:{
            // don't put a ".js" at the end of the file
            'angular' : 'path/angular',
            'underscore' : 'path/underscore-min',
            '...': '...',
        },
        // and other useful things
    });
    

    其次,在同一个文件或另一个文件中(请参阅上面链接中的参数 deps),引导您的应用程序: 就像这里解释的那样:https://docs.angularjs.org/guide/bootstrap

    例子:

    // AMD module injection, more info here : http://requirejs.org/docs/whyamd.html
    define([ // inject all the external library you need + the definition of your app
        'angular',
        'require',
        'yourpath/yourApp' // don't bother here, it's explained in third point
    ], function(angular){
        // link your app to the document file programatically
        angular.bootstrap(document, ['nameOfYourApp']); 
    });
    

    第三,你定义你的应用(在“yourpath/yourApp”中)

    例子:

    define([
        'angular',
        // put all path to your directives + controllers + services
    ], function(angular){ // only specify parameters the library you use in the function
        // you create your sublime app :)
        angular.module('nameOfYourApp', [
            // put all the name of your modules injected above
        ]);
    });
    

    上面的例子是为单页应用程序制作的。 您可以在此处找到多页应用程序的其他示例 http://requirejs.org/docs/start.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-21
      • 2018-05-10
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      • 2014-03-20
      相关资源
      最近更新 更多