用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']);

请问这是什么情况造成的呢?

gulp jade

5152538 9 years, 4 months ago

应该是jade中变量写的有问题,你可以试试: h1 = #{template.nane}

Criki answered 9 years, 4 months ago

Your Answer