我正在研究node.js项目,我正在使用车把模板。
我在router.js中使用partial和helper。
在帮助文件中使用jquery时,会发生以下错误。
*节点错误消息
ReferenceError: $ is not defined
at Object.exports.prnChart (/Volumes/MyStorage/[Project]/myweb/views/helpers/academy/hlp_ac_home.js:22:2)
at Object.eval [as main] (eval at createFunctionContext (/Volumes/MyStorage/[Project]/myweb/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:6:201)
at main (/Volumes/MyStorage/[Project]/myweb/node_modules/handlebars/dist/cjs/handlebars/runtime.js:175:32)
at ret (/Volumes/MyStorage/[Project]/myweb/node_modules/handlebars/dist/cjs/handlebars/runtime.js:178:12)
at ret (/Volumes/MyStorage/[Project]/myweb/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:526:21)
at ExpressHandlebars._renderTemplate (/Volumes/MyStorage/[Project]/myweb/node_modules/express-handlebars/lib/express-handlebars.js:247:12)
at ExpressHandlebars.<anonymous> (/Volumes/MyStorage/[Project]/myweb/node_modules/express-handlebars/lib/express-handlebars.js:173:21)
* app.js
var express = require( 'express' );
var exphbs = require( 'express-handlebars' );
var ac_routes = require( './routes/route' );
var app = express();
app.set( 'views', path.join( __dirname, 'views' ) );
var handlebars = require( './views/helpers/hlp_basic.js' )( exphbs );
app.engine( 'hbs', handlebars.engine );
app.set( 'view engine', 'hbs' );
app.listen( 3000, function () {
console.log( 'Cocle app listening on port 3000!' );
});
* route.js
var express = require( 'express' );
var request = require( 'request' );
var $ = require('jquery'); // not working!!
var router = express.Router();
router.get ( '/', function ( req, res ) {
res.render( 'academy/ac_home', {
helpers: require( __dirHelper + '/academy/hlp_ac_home' ),
title: 'Ac. Home',
layout_lnb: true,
type: 'academy',
page_style: 'academy',
is_chart : true
});
});
module.exports = router;
* hlp_ac_home.js
var $ = require('jquery'); // not working!!
exports.prnChart = function ( target ) {
$.ajax({
type: 'POST',
url: 'http://api.mydomain/list/chart.php',
crossDomain : true,
processData: false,
cache: false,
contentType: 'application/json; charset=UTF-8',
data: jsonReq,
success: function(data) {s
console.log('response data : ',data);
htmlDom = data.list;
}, // Success Function
error: function(xhr, res){
console.log("Error code : " + xhr.status + "\nMessage : " + res);
}
});
return htmlDom;
};
如何在帮助文件(hlp_ac_home.js)中使用jquery?
请帮帮我 - :)