【问题标题】:Browserify with Angular and non-CommonJS librariesBrowserify 使用 Angular 和非 CommonJS 库
【发布时间】:2016-01-21 22:35:05
【问题描述】:

我正在尝试将我的 Angular 项目转换为使用 CommonJS 和 Browserify。不过我的问题是我的一些 Angular 模块不是 CommonJS 格式。包含它们的最佳方式是什么?

例如,在我的 app-js 中,我有:

angular
    .module('app', ['ngRoute', 'ngResource', 'ngCookies', 'ngSanitize',
                    'ngMessages', 'flash', 'smart-table', 'ui.bootstrap',
                    'isteven-multi-select'])

所以对于其中的大多数,我可以只获取 npm 版本并添加 var ngRoute = require(ng-route) 等。一些模块,如 'isteven-multi-select' 没有 CommonJS 版本。如何在我的代码中包含它?

【问题讨论】:

    标签: javascript angularjs node.js browserify commonjs


    【解决方案1】:

    如何使它们成为 commonJs:

    将此添加到您的文件中:

    require('./isteven-multi-select');
    

    并在同一目录中创建 isteven-multi-select.js 文件(您可以更改目录但随后在 require 更改路径)。

    现在在 isteven-multi-select.js 顶部添加:

    module.exports = angular.module('isteven-multi-select',[])
    //rest of the code, for example
    .service('someservice', function () {...})
    .controller('somecontroller', function() {...})
    

    【讨论】:

    • 我宁愿在不改变库的情况下这样做,这样我就不必在每次更新库时都经历这个过程。
    • 这个库是你的吗?如果是,那么您可以添加 package.json 并使其成为 npm 包(您不必更改库文件)。您可以将其用作普通的 javascript 文件或 npm 包。如果此解决方案对应您,请写评论,我将编辑答案并详细写下如何实现它
    • 这不是我的图书馆。
    猜你喜欢
    • 1970-01-01
    • 2019-01-10
    • 2013-07-16
    • 1970-01-01
    • 2015-04-06
    • 2015-01-25
    • 1970-01-01
    • 2014-12-23
    相关资源
    最近更新 更多