使用pug-html-loader将数据传递给pug(无法读取undefined的属性)
问题描述:
根据自述文件pug-html-loader,加载程序接受data
对象,该对象正在作为options
的一部分传递给模板。虽然options
在pug API reference内不可用,但我发现的一个grunt插件(grunt-contrib-pug)以相同的方式使用API。使用pug-html-loader将数据传递给pug(无法读取undefined的属性)
我指定的装载机以下选项:
loader: 'pug-html-loader',
options: {
pretty: true,
exports: false,
debug: !env.production,
compileDebug: !env.production,
cache: config.build.useCaching,
doctype: 'html',
data: {
title: config.metadata.title,
baseUrl: config.build.baseUrl,
server: env.server,
metadata: config.metadata
}
}
其中我想访问以下方式,根据this question:
title= data.title
但是,我总是碰上编译时出现以下错误:
ERROR in Error: Child compilation failed:
Module build failed: TypeError: Cannot read property 'title' of undefined
为了调试的目的,我们公司luded模板下面一行:
-
console.log(' DATA! ' + data);
导致:
DATA! undefined
我还保证了数据的正确传递通过声明一些乱码,而不是对象的帕格(例如一个字符串,普通的json,一个数字......),那么帕格编译器会抱怨数据不是有效格式等。
任何人都面临同样的问题?
答
您应该引用数据directy(即无前缀与data
领域)
所以,你应该改变你的代码如下所示:
{
loader: 'pug-html-loader',
options: {
data: {
title: 'Hello World'
}
}
}
然后从你的哈巴狗模板引用它
doctype html
html
head
title= hello