【问题标题】:in node.js how do you pass a "require" object to jade?在 node.js 中,如何将“require”对象传递给玉?
【发布时间】:2016-03-14 07:41:47
【问题描述】:

我创建了一个节点模块,其中包含一个简单的 js 文件。我想在我的玉文件中使用这个 js 函数。在 music.js 中,我需要模块“myplayer”:

var keystone = require('keystone');
exports = module.exports = function(req, res) {
   var view = new keystone.View(req, res);
var locals = res.locals;
    locals.myplayer = require('myplayer');
    // locals.section is used to set the currently selected
// item in the header navigation.
    locals.section = 'music';
// Render the view
view.render('music');
};

我知道它正在访问这个文件,因为当我在浏览器中访问 music.jade 时,我的终端中出现的 myplayer js 文件中包含一个控制台打印语句。 然后在我的 music.jade 文件中,我尝试使用 myplayer 函数 myplayer().tryplay。

extends ../layouts/default
block content
.container: .jumbotron
    img(src='/images/logo.svg', width='160')
    h1 Welcome
    audio#test(controls='')
        source(src='/mymusic/cold.mp3')
        | Your browser does not support the audio element.
    button Click me
        if (section == 'music')
            li sees variable
    p The Music page is currently under construction
    hr
block js        
script.
    $(document).ready(function(){
    $("button").click(myplayer().tryplay)   
    });

似乎我正确地传递了变量,因为我所做的与 locals.section 完全相同,这会影响“看到变量”是否被打印。

我得到的错误是“Reference Error: myplayer is not defined”

所以我有很多问题。 “if (section == 'music')”语句实际上是 jquery 命令,而不是 css?但我没有看到“$”或任何表示它的东西。

“Section”显然是值“music”的变量,但我不知道如何在 music.jade css 脚本或内部 javascript 中将其打印出来。

最后当然是,为什么“$("button").click(myplayer().tryplay)" 找不到对象myplayer?

看起来我只是在犯简单的语法错误,但我担心我真的迷路了。

感谢您的帮助。


这就是我要重现的内容,但使用 node_modules:

extends ../layouts/default
block content
.container: .jumbotron
img(src='/images/logo.svg', width='160')
h1 Welcome
audio#test(controls='')
    source(src='/mymusic/cold.mp3')
    | Your browser does not support the audio element.
    button Click me
    p The Music page is currently under construction
    hr
block js        
script(src='/jplay/my_jquery.js')
script.
    $(document).ready(function(){
    $("button").click(tryplay)
    });

这是 my_jquery.js 文件:

var tryplay = function(){
    $("#test")[0].play();}

以上 2 个文件工作正常。 我创建的 myplayer node_modules 主文件是:

var exports = module.exports = {} ; 
function startup () {
var tryplay = function(){
    $("#test")[0].play();}
console.log ("runUserScript");
}
startup ();

我不打算创建一个字符串,我想将函数传递给 music.jade。显然,使用 locals.myplayer 不正确?


所以我把music.js改成:

    var keystone = require('keystone');
    exports = module.exports = function(req, res) {
    var myplayer = require('myplayer');
    var view = new keystone.View(req, res);
    var locals = res.locals;
   //  locals.myplayer = require('myplayer');
    // locals.section is used to set the currently selected
    // item in the header navigation.
    locals.section = 'music';

    // Render the view
    view.render('music');
    };

我将 music.jade 更改为:

extends ../layouts/default
block content
.container: .jumbotron
    img(src='/images/logo.svg', width='160')
    h1 !{section}
    audio#test(controls='')
        source(src='/mymusic/cold.mp3')
        | Your browser does not support the audio element.
    button Click me
    p The Music page is currently under construction
    hr
block js        
script.
    $(document).ready(function(){
    $("button").click(function(){myplayer().tryplay})   
    });

但是现在我得到“ReferenceError: myplayer is not defined”,所以我什么都没有。

【问题讨论】:

  • 我很困惑,您正在使用require 导入代码,然后尝试将其用作本地变量的字符串? myplayer 模块导出什么?

标签: jquery node.js


【解决方案1】:

看完这篇文章后: Script path for ExpressJS in node_modules 我意识到我一开始就做对了。您的客户端 javascript 文件应该位于 public 文件夹中,而不是 node_modules 文件夹中。如果您愿意,上面的链接描述了如何访问 node_modules 中的文件。 我尝试这样做的原因是因为我使用“npm install”下载了 jplayer,为了让它运行,我必须将 jquery.jplayer.js 文件复制到公共目录,这似乎不正确。 我应该做的是使用这两个网站建议的“凉亭”? https://github.com/happyworm/jPlayer 和 How to include scripts located inside the node_modules folder?

【讨论】:

    猜你喜欢
    • 2016-11-15
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 2020-04-26
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    相关资源
    最近更新 更多