用gulp编译jade报错无法读取变量
这是index.jade
<!DOCTYPE html>
html(lang="en")
head
meta(charset="UTF-8")
title Document
body
- var templates = [{'name': 'foo'}]
each template in templates
include template
这是index中include的template
h1= template.name
编译出来的index.html文件也是正确的
<!DOCTYPE html>
<html lang="en"></html>
<head>
<meta charset="UTF-8"/>
<title>Document</title>
</head>
<body>
<h1>foo</h1>
</body>
但是编译出来的template.html文件是空的
而且在编译后gulp总是会报错
vents.js:85
throw er; // Unhandled 'error' event
^
TypeError: /Users/wang/PrivateWork/express_i/app/html/template.jade:1
> 1| h1= template.name
Cannot read property 'name' of undefined
at eval (eval at <anonymous> (/Users/wang/PrivateWork/express_i/node_modules/gulp-jade/node_modules/jade/lib/index.js:218:8), <anonymous>:12:65)
at eval (eval at <anonymous> (/Users/wang/PrivateWork/express_i/node_modules/gulp-jade/node_modules/jade/lib/index.js:218:8), <anonymous>:17:22)
at res (/Users/wang/PrivateWork/express_i/node_modules/gulp-jade/node_modules/jade/lib/index.js:219:38)
at DestroyableTransform.CompileJade [as _transform] (/Users/wang/PrivateWork/express_i/node_modules/gulp-jade/index.js:34:50)
at DestroyableTransform.Transform._read (/Users/wang/PrivateWork/express_i/node_modules/gulp-jade/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:172:10)
at DestroyableTransform.Transform._write (/Users/wang/PrivateWork/express_i/node_modules/gulp-jade/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:160:12)
at doWrite (/Users/wang/PrivateWork/express_i/node_modules/gulp-jade/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:326:12)
at writeOrBuffer (/Users/wang/PrivateWork/express_i/node_modules/gulp-jade/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:312:5)
at DestroyableTransform.Writable.write (/Users/wang/PrivateWork/express_i/node_modules/gulp-jade/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:239:11)
at write (/Users/wang/PrivateWork/express_i/node_modules/gulp/node_modules/vinyl-fs/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:623:24)
gulp中这么写的
gulp.task('server', function(){
connect.server({
livereload: true,
port: 3000
});
});
gulp.task('jade', function(){
gulp.src('./app/html/*.jade')
.pipe(jade({
pretty: true
}))
.pipe(gulp.dest('./build/html'))
.pipe(connect.reload());
});
gulp.task('watch', function(){
gulp.watch(['./app/html/*.jade'], ['jade']);
});
gulp.task('default', ['server', 'watch', 'jade']);
请问这是什么情况造成的呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该是jade中变量写的有问题,你可以试试: h1 = #{template.nane}
在编译template.jade这个文件的时候,jade找不到name,因此无法编译template.name
在编译index.jade的时候,因为其中include引入了模板template.jade,而在include以前,index.jade中就存在了
- var templates = [{'name': 'foo'}]
因此可以编译正确index.jade