【问题标题】:gulp-jade reports error like 'Cannot read property 'xxx' of undefined'gulp-jade 报告错误,例如“无法读取未定义的属性 'xxx'”
【发布时间】:2016-01-14 20:41:37
【问题描述】:

当我使用 gulp-jade@1.1.0 编译“index.jade”时

<!DOCTYPE html>
html(lang="en")
head
    meta(charset="UTF-8")
    title Document
body
    - var json = {'foo': 'bar'}
    include template

包括“template.jade”

h1= json.foo

gulp-jade 总是报这样的错误

[15:11:34] Starting 'jade'...
[15:11:34] Starting 'sass'...
[15:11:34] Finished 'sass' after 2.68 ms
[15:11:34] Plumber found unhandled error:
 TypeError in plugin 'gulp-jade'
Message:
    C:\Users\Administrator\Desktop\workspace\node\src\jade\template.jade:1
  > 1| h1= json.foo

Cannot read property 'foo' of undefined
Details:
    path: C:\Users\Administrator\Desktop\workspace\node\src\jade\template.jade
[15:11:34] Finished 'jade' after 19 ms

但是编译后的html文件是对的

<!DOCTYPE html>
<html lang="en"></html>
<head>
  <meta charset="UTF-8"/>
  <title>Document</title>
</head>
<body>
  <h1>bar</h1>
</body>

我的 gulpfile 是这样写的

gulp.task('jade', function(){
    return gulp.src('src/jade/*.jade')
        .pipe(plumber())
        .pipe(gulpJade({
            jade: jade,
            pretty: true
        }))
        .pipe(gulp.dest('build/html'))
        .pipe(connect.reload());
});

【问题讨论】:

    标签: node.js gulp pug


    【解决方案1】:

    Gulp 尝试分别编译每个模板。

    您必须只指定入口点 (index.jade):

    gulp.task('jade', function(){
        return gulp.src('src/jade/index.jade')
            .pipe(plumber())
            .pipe(gulpJade({
                jade: jade,
                pretty: true
            }))
            .pipe(gulp.dest('build/html'))
            .pipe(connect.reload());
    });
    

    或从 gulp.src

    中排除 template.jade

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 2017-09-05
      • 2019-02-05
      • 1970-01-01
      • 2016-08-15
      相关资源
      最近更新 更多