为什么我不能在玉器中找到app.locals
问题描述:
我正在使用express/node/jade并为某些变量构建模块。为什么我不能在玉器中找到app.locals
我在app.js app.locals.GVs = require('./utils/globalvars.js');
这条线,这是我的玉
extends ../layout
block content
h1 Edit - Change Your Profile
p There will hopefully be a form to change users here.
form(id='form',action='')
include ./includes/user-form.jade
input(type='reset', value='Clear')
input(type='submit', value='Update')
script.
//Wait for document to load.
$(document).ready(function(){
$('#firstname').val('#{data.firstname}');
$('#lastname').val('#{data.lastname}');
$('#username').val('#{data.username}');
$('#phone').val('#{data.phone}');
$('#email').val('#{data.email}');
//Once form is submitted thisUserData (UserData) is created.
$('#form').submit(function(){
// creates the variable thisUserData as a new UserData.
var thisUserData = new GVs.UserData($('#firstname').val(), $('#lastname').val(), $('#username').val(), $('#password').val(), $('#confirmpassword').val(), $('#phone').val(), $('#email').val());
//POST call; Sends thisUserData and alerts that a user was updated.
$.post('/user/edit', {userFormData: thisUserData}, function(data){
alert("User updated!");
});
});
});
我,当我打电话GVS var thisUserData = new GVs.UserData(...);
我也试图通过它上得到一个错误与“数据”相同的方式,但它做了同样的事情。
编辑:我想看看我是否能在玉文件中获取GVS像这样
p There will hopefully be a form to change users here.
成为本
p There will hopefully be a form to change users here. '#{GVs.UserData}'
和它的存在,我为什么能得到它在玉,但不在脚本中?
答
您需要在客户端JS和服务器端之间传递变量,因为客户端并不知道node.js全局变量。例如:
extends ../layout
block content
h1 Edit - Change Your Profile
p There will hopefully be a form to change users here.
form(id='form',action='')
include ./includes/user-form.jade
input(type='reset', value='Clear')
input(type='submit', value='Update')
script.
var GVs = !{JSON.stringify(GVs)};
//Wait for document to load.
$(document).ready(function(){
$('#firstname').val('#{data.firstname}');
$('#lastname').val('#{data.lastname}');
$('#username').val('#{data.username}');
$('#phone').val('#{data.phone}');
$('#email').val('#{data.email}');
//Once form is submitted thisUserData (UserData) is created.
$('#form').submit(function(){
// creates the variable thisUserData as a new UserData.
var thisUserData = new GVs.UserData($('#firstname').val(), $('#lastname').val(), $('#username').val(), $('#password').val(), $('#confirmpassword').val(), $('#phone').val(), $('#email').val());
//POST call; Sends thisUserData and alerts that a user was updated.
$.post('/user/edit', {userFormData: thisUserData}, function(data){
alert("User updated!");
});
});
});
这适用于我在该模块中具有的变量,但无法访问其中的任何函数。 – Devcon
@Devcon如果你想在客户端和服务器上使用相同的功能,你可以使用browserify http://browserify.org/ – vanadium23
感谢@ vanadium23的帮助我使用了webpack。 – Devcon