我正在努力学习node.js。我希望能够使用vash视图引擎,但由于某些原因,我无法使其工作。我看了一个关于Pluralsight的教程,并引用了vash文档,但我一直收到同样的错误。这是我的代码
server.js:
var http = require('http');
var port = process.env.port || 1337;
var express = require("express");
var app = express();
app.set("view engine", "vash");
app.get("/", function (req, res) {
res.render("index", { title: "Express + Vash" });
});
var server = http.createServer(app);
server.listen(port);
layout.vash:
<!DOCTYPE html>
<html>
<head>
<title>@model.title</title>
</head>
<body>
<div>@html.block("body")</div>
</body>
</html>
index.vash
@html.extend('views/layout', function(model) {
@html.block("body", function(model) {
<h1>@model.title</h1>
<p>This id from the server</p>
})
})
堆叠跑道:
TypeError: undefined is not a function
at View.render (C:UsersMichaelDesktopNode_StuffLearnNodeTheBoardnode_modulesexpresslibview.js:93:8)
at EventEmitter.app.render (C:UsersMichaelDesktopNode_StuffLearnNodeTheBoardnode_modulesexpresslibapplication.js:566:10)
at ServerResponse.res.render (C:UsersMichaelDesktopNode_StuffLearnNodeTheBoardnode_modulesexpresslibresponse.js:938:7)
at C:UsersMichaelDesktopNode_StuffLearnNodeTheBoardserver.js:9:9
at Layer.handle [as handle_request] (C:UsersMichaelDesktopNode_StuffLearnNodeTheBoardnode_modulesexpresslibrouterlayer.js:82:5)
at next (C:UsersMichaelDesktopNode_StuffLearnNodeTheBoardnode_modulesexpresslibrouterroute.js:110:13)
at Route.dispatch (C:UsersMichaelDesktopNode_StuffLearnNodeTheBoardnode_modulesexpresslibrouterroute.js:91:3)
at Layer.handle [as handle_request] (C:UsersMichaelDesktopNode_StuffLearnNodeTheBoardnode_modulesexpresslibrouterlayer.js:82:5)
at C:UsersMichaelDesktopNode_StuffLearnNodeTheBoardnode_modulesexpresslibrouterindex.js:267:22
at Function.proto.process_params (C:UsersMichaelDesktopNode_StuffLearnNodeTheBoardnode_modulesexpresslibrouterindex.js:321:12)
.vash文件位于项目根目录下的视图文件夹中。
提前感谢!
根据此vash问题,以下内容应该有效:
var vash = require('vash');
vash.__express = vash.renderFile;
app.set("view engine", "vash");
在index.vash中,只需使用'/layout'而不是'views/layout'。
即:
@html.extend(**'/layout'**, function(model) { ... });