問題描述
我的手很頭疼.這是我當前的設置:
I have a headache on my hands. Here's my current setup:
- 獲取供應商庫(在本例中為 Angular)
- 運行 browserify 的 gulp 任務
- debowerify 使 bower 庫與 browserify 兼容
App.js(在 browserify 之前):
'use strict';
var angular = require("angular");
var Routes = require("./routes");
angular.module('MyAngularApp')
.config(Routes);
App.js(在 browserify 之后/在 bundle.js 中):
var angular = require("./../ext/angular/angular.js");
var Routes = require("./routes");
angular.module('MyAngularApp')
.config(Routes);
到目前為止一切都很好,對吧?似乎 debowerify 完成了它的工作,并將 angular
替換為來自 bower 的 angular.js
的相對路徑.
So far so good, right? It seems like debowerify did it's job and replaced the angular
with the relative path to the angular.js
from bower.
但是當我在瀏覽器命令行中調試 bundle.js
時,在執行了前兩行 require
之后(對于 angular
和 Routes
),angular
是一個空 obj,但 Routes
正是我在導出中設置的正確函數.
But when I debug the bundle.js
in the browser command line, after executing the first two require
lines (for angular
and Routes
), angular
is an empty obj, but Routes
is exactly the correct function that I setup in the export.
問題:為什么使用 require
函數不能正確導入 angular
?
Question: Why isn't angular
being imported correctly using the require
function?
我將它放入我的 package.json
以使 debowerify
工作:
I put this into my package.json
to get the debowerify
working:
"browserify": {
"transform": [
"debowerify"
]
},
推薦答案
AngularJS 目前不支持 CommonJS,所以 var angular = require("angular")
不起作用.而不是它,只使用 require('angular')
.
AngularJS doesn't support CommonJS at the moment, so var angular = require("angular")
doesn't work. Instead of it, use just require('angular')
.
'use strict';
require('angular');
var Routes = require("./routes");
angular.module('MyAngularApp')
.config(Routes);
Angular 對象將被全局加載,它也將能夠被其他 JS 文件訪問.
The Angular object will be loaded globally and it'll be able to be accessed by other JS files, too.
這篇關于無法使用 browserify 和 debowerify 獲取外部庫的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!