【问题标题】:My javascript module is not working when I used gulp and browserify当我使用 gulp 和 browserify 时,我的 javascript 模块不工作
【发布时间】:2015-07-23 14:26:30
【问题描述】:

当我使用 gulp 和 browserify 连接 javscript 文件时,我的 javascript 模块无法正常工作。

这是我的模块。

LastPost.js

var PostBox = require('../objects/PostBox');

var LastestPosts = (function() {
    var btnLeft;
    var btnRight;

    var postBoxs = (function() {
        var arr = new Array();
        for (var i=0; i<4; i++) {
            var post_box = new PostBox();
            arr.push(post_box);
        }
        return arr;
    })();

    var index = 0;
    var lastIndex;


    return({
        initDOM: function($) {
            btnLeft = $('#btn-lastest-previous');
            btnRight = $('#btn-lastest-next');
            /* some codes */
        },
        initIndex: function() {
            if (index == 0) {
                /* some codes */
            }
            else if (index == lastIndex) {
                /* some codes */
            }
            else {
                /* some codes */
            }
        },
        getLastIndex: function() {
             $.get('/lastestposts-getlastindex', function(data) {
                lastIndex = data;
             });
        },
        updatePostBox: function() {
            var parameter = { index: index };

            $.get('/lastestposts-getindex', parameter, function(data) {
                /* some codes */
            });
        },
        increaseIndex: function() {
            if (index < lastIndex) {
                index++;
             }
         },
        decreaseIndex: function() {
            if (index > 0) {
                index--;
            }
         },
        getLeftBtn: function() {
             return btnLeft;
        },
        getRightBtn: function() {
            return btnRight;
        }
    });
 })();

 module.exports = LastestPosts;

Main.js

var LastestPosts = require('./module/LastestPosts');

$(document).ready(function() { 
    LastestPosts.initDOM($);
    LastestPosts.getLastIndex();
    LastestPosts.updatePostBox();
    LastestPosts.initIndex();

    (LastestPosts.getRightBtn()).click(function() {
        LastestPosts.increaseIndex();
        LastestPosts.initIndex();
        LastestPosts.updatePostBox();
    });
    (LastestPosts.getLeftBtn()).click(function() {
        LastestPosts.decreaseIndex();
        LastestPosts.initIndex();
        LastestPosts.updatePostBox();
    });
});

btnLeftbtnRight 可以正常点击,但 LastestPosts.increaseIndex()LastestPosts.initIndex() 无法正常工作。

在使用 gulp 和 browserify 之前,我会像这样导入每个 javascript 文件。

<script src="./js/objects/PostBox.js"></script>
<script src="./js/module/Lastestposts.js"></script>
<script src="./js/main.js"></script>

这很好用,但是在我使用 gulp 和 browserify 连接 javascript 文件后,我的 javascript 模块不起作用...

这是我的 gulp 设置。

gulp.task('build-js', function () {
    return browserify('./public/src/js/main.js')
        .bundle()
        .pipe(source('bundle.js'))
        .pipe(gulp.dest('./public/dist/js'));
});

请帮助这个小学生,并为我糟糕的英语水平感到抱歉。我英语不好:'(

【问题讨论】:

  • 好像我缺少一些代码。你在某个地方的回购中拥有一切吗?我会在第二天左右看看。
  • 非常感谢 :) 你救了我。这是 git 仓库。 github.com/seokju-na/blog-bang-extend.git

标签: javascript node.js module gulp browserify


【解决方案1】:

不确定你到底哪里出了问题,但问题归结为终端中的这个错误 -

express deprecated res.send(status): Use res.sendStatus(status) instead app.js:107:9

这意味着最终问题在于这个特定函数没有返回lastIndex

getLastIndex: function() {
    $.get('/lastestposts-getlastindex', function(data) {
        lastIndex = data;
    });
},

如果您将res.send(4) 修改为res.send({lastIndex: 4}); 并将getLastIndex() 修改为

getLastIndex: function() {
    $.get('/lastestposts-getlastindex', function(data) {
        lastIndex = data.lastIndex;
    });
},

不知道为什么它之前工作 - 但目前你的 lastIndex 私有变量是 undefined 破坏一切 - 这就是原因。

【讨论】:

  • 哇!它工作得很好!您对我的网络学习提供了很多帮助。非常感谢!
【解决方案2】:

此问题通常出现在“不干净”的内联 cmets 中。

例如:

/*console.log('this will work');*/

/*

console.log('this might not work when minified');
*/

尝试删除所有 cmets 并再次缩小代码。

【讨论】:

  • 抱歉,看了几眼您的代码后,我看不到任何可能导致您的代码在缩小时无法工作的语法错误。您是否收到任何控制台错误,这些错误可能指向在缩小时会导致问题的行?
  • 不...我没有收到任何错误。这很奇怪,因为当文件分离时它工作得很好,但是在连接文件之后, LastestPosts.increaseIndex() 和 LastestPosts.initIndex() 不起作用。(索引没有改变)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-03
相关资源
最近更新 更多