将值从javascript传递到jade
我使用node.js、express、jade和socket.io,我可以在jade端运行javascript代码,但我无法从脚本生成html。挡块 我必须根据您的输入更新我的问题。以下是文件: server.js将值从javascript传递到jade,javascript,node.js,express,socket.io,pug,Javascript,Node.js,Express,Socket.io,Pug,我使用node.js、express、jade和socket.io,我可以在jade端运行javascript代码,但我无法从脚本生成html。挡块 我必须根据您的输入更新我的问题。以下是文件: server.js app.get('/', function (req, res) { res.sendfile(__dirname + '/index.html'); }); io.on('connection', function (socket) { socket.emit(
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.emit('news', { hello: res}); // res is the reponse object
socket.on('my other event', function (res) {
console.log("socket.io connected and data sent to jade");
});
});
layout.jade:
doctype html
html
head
title= title
script(src='components/jquery/dist/jquery.min.js')
script(type='text/javascript' src='https://cdn.socket.io/socket.io-1.0.6.js')
script(type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/jade/1.11.0/jade.min.js')
script(type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/jade/1.11.0/runtime.min.js')
script.
var socket = io.connect('http://localhost:8898/');
socket.on('news', function (data) {
var photo = data.hello.data[0].images.original.url;
});
body
block content
img(src="#{photo}") // <--- issue here, creates "undefined" image
doctype html
html
头
头衔
脚本(src='components/jquery/dist/jquery.min.js')
脚本(type='text/javascript'src='s)https://cdn.socket.io/socket.io-1.0.6.js')
脚本(type='text/javascript'src='s)https://cdnjs.cloudflare.com/ajax/libs/jade/1.11.0/jade.min.js')
脚本(type='text/javascript'src='s)https://cdnjs.cloudflare.com/ajax/libs/jade/1.11.0/runtime.min.js')
剧本
var socket=io.connect('http://localhost:8898/');
socket.on('news',函数(数据){
var photo=data.hello.data[0].images.original.url;
});
身体
块内容
img(src=“#{photo}”)/您可以删除现有组件内容,然后在jQuery AJAX回调中使用jQuery重新渲染。类似于
杰德:
标签#数据
之后:
剧本
var socket = io.connect('http://localhost:8898/');
socket.on('news', function (data) {
$('#data').text('');
$('#data').text(data);
});
可能有点太明显了,但从这个例子来看,我认为脚本标记中的JS块需要缩进。但我还没能测试一下
script.
var socket = io.connect('http://localhost:8898/');
socket.on('news', function (data) {
console.log("socket.io.on data reaching jade");
console.log(data); // prints fine, but to console only.
socket.emit('my other event', { my: data });
});
因为您是从layout.jade和index.jade扩展而来的,所以jade是您的子模板。您不需要声明html是您的块内容吗?像这样:
extends layout
block content
#{data} // my problem is here, creating <undefined> tags in html
p #{data.stuff}
img(src="images/bird.jpg") // works
扩展布局
块内容
#{data}//我的问题是,在html中创建标记
p#{data.stuff}
img(src=“images/bird.jpg”)//有效
也许吧,但是我从express生成的jade模板完全消失了。这似乎没有什么区别。我根据您的输入更新了我的问题。请检查layout.jade最后一行中的注释。错误为“无法读取未定义的属性'hello'”
extends layout
block content
#{data} // my problem is here, creating <undefined> tags in html
p #{data.stuff}
img(src="images/bird.jpg") // works