空对象需要角度浏览器 [英] Empty object require angular browserify

查看:43
本文介绍了空对象需要角度浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用 bowserify、angularjs 和 coffeescript 时遇到了问题.

I m facing a problem when I try to use bowserify, angularjs and coffeescript.

事实上,当我尝试 require('angular') 时,我得到一个空对象:

In fact, when I try to require('angular'), I get an empty object :

angular = require('angular')
console.log angular ## return an empty object {}

configuration = require('../../config/config') 
console.log configuration ## returns my fully config file correctly

我不知道为什么在这种情况下它不能正常工作:-/.这是我的 package.json,我在其中放置了我的个人角度依赖项:

I dont know why it doesn't work properly in this case :-/. This is my package.json where I put the my personnal angular dependencies :

{
    "dependencies": {
        "gulp": "*",
        "gulp-browserify": "*",
        "coffeeify": "*",
        "gulp-concat": "*"
    },
    "browser": {
        "angular": "./app/core/angular-libs/angular.min.js",
        "angular-route": "./app/core/angular-libs/angular-route.min.js",
        "angular-animate": "./app/core/angular-libs/angular-animate.min.js"
    }
}

这是我的 gulp 文件,它在 dest 文件夹中生成我的 bundle.js:

And this is my gulp file, that generates my bundle.js in dest folder :

var gulp = require('gulp');
var browserify = require('gulp-browserify');
var concat = require('gulp-concat');

gulp.task('scripts', function () {
    return gulp.src('app/**/*.coffee', { read: false })
        .pipe(browserify({ transform: ['coffeeify'], extensions: ['.coffee'] }))
        .pipe(concat('bundle.js'))
        .pipe(gulp.dest('./dest/'));
});

gulp.task('default', function () {
    gulp.run('scripts');
});

你能帮帮我吗?:-/

感谢提前

推荐答案

Angular 1 不支持 CommonJS 模块,所以它导出"一个空对象.

Angular 1 doesn't support CommonJS modules, so it 'exports' an empty object.

相反,只需要它(不分配结果):

Instead, just require it (without assigning the result):

require('angular')

这会将角度附加到全局对象.

This will attach angular to the global object.

更新:从 Angular 1.3.14 开始,require('angular') 现在返回 angular 对象.

UPDATE: As of Angular 1.3.14, require('angular') now returns the angular object.

这篇关于空对象需要角度浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆