Jade:加载外部javascript和调用函数 [英] Jade: load external javascript and call function

查看:207
本文介绍了Jade:加载外部javascript和调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Express / Node / Jade,现在在Jade文件中我想从公用文件夹中只包含一个javascript文件。
例如,在jade文件中,我键入以下内容:

I was learning Express/Node/Jade and now in the Jade file I want to include a javascript file from the public folder just for the page. For example, in jade file I type this:

script(src='/javascripts/test.js')

和test.js里面我有一个函数

and inside test.js I have a function

function check_test(){
    return "It's working!"
}

然后我尝试通过

- var test_response = check_test()

比我得到错误说undefined不是一个功能,test.js根本没有加载。
显然,Jade不加载文件,它们只能转换为HTML代码。

than I got the error saying that "undefined is not a function" and test.js isn't load at all. Apparently Jade doesn't load the file, they only transform into HTML code.

我看着别人的问题,这是我能找到的最接近的问题,但它并没有提供明确的答案。

I look someone else's question and this is the closest one I can found but it doesn't provide a clear answer of what to do. In Jade, how can you call a function in an external Javascript

所以我的问题是:在这种情况下,我该怎么做才能使它工作?

So my question is: In this case what should I do to make it work?

我不想在layout.js中加载文件,因为我只希望test.js只能被这个页面使用。

I don't want to load the file in layout.js since I only want test.js only be use by this page.

推荐答案

嗯...首先,在浏览器中发生在服务器上的情况是不同的。所以Jade是HTML的渲染,所以如果你在浏览器中。这是ExpressJS运输,即渲染玉。如果你想打电话,你的HTML Javascript(Rendering of Jade)应该会显示你在哪里。对于示例

Well... In the first instance, it is different what happens in the browser of what happens on the server. So Jade is a rendering of HTML, therefore if you are in the browser. It's what ExpressJS shipping, ie rendering Jade. If you want to call, your HTML Javascript (Rendering of Jade), should show you where the Javascript. For exmaple

在Server.js

in Server.js

// Get the Javascript in the browser
app.use("/javascripts", express.static("./outJavascripts"));
// Get the URL
app.all("/", function(req, res){
  // Render the Jade and Send for the client (Browser)
  req.render("myTemplate.jade");
});

在myTemplate.jade

In myTemplate.jade

script(src='/javascripts/test.js')

./outJavascripts/test.js

In "./outJavascripts/test.js"

function check_test(){
    console.log("It's working! :D");
    return "It's working!";
}

如果你这样做,你会发现它是运行的,文件 ./outJavascripts/test.js在浏览器中。而且check_test函数从不在服务器中运行。

If you do this, you will realize that it is run, the file "./outJavascripts/test.js" in the browser. And the function "check_test" never run in the server.

这篇关于Jade:加载外部javascript和调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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