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