我在jade中编写js函数时遇到了困难。我总是得到函数没有定义错误。以下是我的代码:
extends layout
block content
script(src='/socket.io/socket.io.js')
script.
var scoket = io.connect('http://localhost:3000')
var email = #{email}
var userEmail = #{userEmail}
function sendInvite(){ socket.emit('invite from', email, userEmail)}
div(data-role = 'page')
div(data-role = 'header')
h1 #{title}
div(data-role = 'content')
form(action='/postSendInvite', method='post')
input(type = 'hidden', name='_csrf', value='token')
input(type='hidden', name='email', value=email)
input(type='hidden', name='userEmail', value=userEmail)
h1 #{email}
button(type='submit', onclick='sendInvite()') Send Invite
include partials/footer
请让我知道我做错了什么
答案 0 :(得分:1)
请注意,它与玉无关。一旦解释了Javascript,jade就已经呈现为HTML,所以问题是在HTML页面中定义JS函数。
错误可能是因为您定义了一个“scoket”,并且您之后使用的是未定义的“套接字”。
答案 1 :(得分:0)
感谢指出错字,但问题不是拼写错误,它是说sendInvite()没有定义,我发现解决方法是在helper js文件中写入函数,然后将js文件包含到jade中