我正在使用 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/