javascript - 与 Browserify 捆绑时,是否可以在 AngularJS 中使用指令 templateUrls 的相对路径?

标签 javascript angularjs browserify

我正在使用 AngularJS 开发 Web 应用程序,使用 Browserify 将我的 JS 文件捆绑到一个包中以供在页面中使用。目前我的结构是这样的:

app
|-index.html
|-index.js
|-bundle.js
|-components
  |-module1
    |-index.js
    |-module1.directive.js
    |-module1.html

因为捆绑的 javascript 文件最终位于 app 文件夹中,所以我的指令最终看起来像这样:

module.exports = angular.module('module1.directive', [])
    .directive('moduleDirective', function() {
        return {
            restrict: 'E',
            templateUrl: './components/module1/module1.html'
        );
    });

这在一般情况下工作得很好,但如果在更多开发人员参与后项目变得足够复杂并且以这种方式定义指令时移动指令有点困难(更改 templateUrl 路径是一个相对容易忘记的额外步骤)。

我想知道的是,有没有办法让我在 templateUrl 中使用相对路径,以便 Browserify 在打包文件时能够正确解析它?

编辑: 如果有帮助,我可以找到一种方法来获取捆绑在模块本身内部的脚本的原始文件路径,但我不知道如果 Browserify 支持的话。

最佳答案

pathmodify 插件可以解决您的问题吗?更多信息可以在 github ( https://github.com/jmm/pathmodify ) 上找到

var pathmodify = require('pathmodify');
browserify()
     .plugin(pathmodify(), {mods: [
        // Make code like `require('app/something')` act like
        // `require('/somedir/src/something')`
        pathmodify.mod.dir('app', '/somedir/src')
]})

关于javascript - 与 Browserify 捆绑时,是否可以在 AngularJS 中使用指令 templateUrls 的相对路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29372729/

相关文章:

javascript - 更改 javascript 正则表达式分隔符

javascript - 在 for 循环中获取数据(不是索引)

node.js - 如何使用 Gulp 通过 Browerify 从 Bower 组件创建一个单独的供应商包

javascript - 用于混合风格项目的 browserify (grunt, reactjs + plain js)

jquery - 如何构建我的 gulp 文件?

javascript - React-router:在链接和 map 匹配数据中传递 Prop

javascript - 无法将超大图像与 div 的中心对齐

javascript - 当范围值发生变化时,如何使用 angularJs 更新 UI?

css - 带有溢出的模态中的 ui-select 下拉菜单

javascript - AngularJS 表达式中的评估函数