Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用pug文件在html中显示JS文件中的变量_Javascript_Node.js_Pug - Fatal编程技术网

Javascript 使用pug文件在html中显示JS文件中的变量

Javascript 使用pug文件在html中显示JS文件中的变量,javascript,node.js,pug,Javascript,Node.js,Pug,我有两个文件。一个是哈巴狗档案: doctype html html head meta(charset='utf-8') meta(name='viewport', content='width=device-width, initial-scale=1, shrink-to-fit=no') link(href='https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css', rel

我有两个文件。一个是哈巴狗档案:

doctype html
html
head
    meta(charset='utf-8')
    meta(name='viewport', content='width=device-width, initial-scale=1, shrink-to-fit=no')
    link(href='https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css', rel='stylesheet')
    title Your first page
body
    script(src='https://code.jquery.com/jquery-3.3.1.min.js')
    script(src='https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js')
    script(src='https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js')
第二个是JS文件:

var express=require('express'),
记录器=需要('morgan');
var-app=express();
var x=1;
变量y=2;
app.set('views','u dirname+'/views');
应用程序集(“查看引擎”、“帕格”);
应用程序使用(记录器(“开发”);
app.use(express.static(uu dirname+/public));
app.get('/',函数(req,res){
res.render('index',{pretty:true});
});
app.listen(3000,函数(){
log(“应用程序在端口3000上可用”);

});在代码中,您已经向模板(pug)传递了一个变量。你可以用同样的方法来做:

app.get('/', function (req, res) {      
   res.render('index', {pretty:true, x: x, y: y}); 
});
x:
是“键”,而
x
是对变量x的引用。与y相同的程序

在您的pug文件中,例如:

h1 = x
h2 = y
希望我能帮忙。

试试这个

  • 将变量传递给视图
  • 然后在“/public/views”中的模板文件“index.pug”中回显
  • 希望有帮助

    app.get('/', function (req,res) { 
        res.render('index', { pretty: true, title: 'Hey', message: 'Hello there!'}); 
    });
    
    doctype html
    html
    head
        meta(charset='utf-8')
        meta(name='viewport', content='width=device-width, initial-scale=1, shrink-to-fit=no')
        link(href='https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css', rel='stylesheet')
        title Your first page
        title= title
    body
        script(src='https://code.jquery.com/jquery-3.3.1.min.js')
        script(src='https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js')
        script(src='https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js')
    
        h1=message