Javascript 使用pug html加载程序将数据传递给pug(无法读取未定义的属性)
根据的自述文件,加载程序接受一个Javascript 使用pug html加载程序将数据传递给pug(无法读取未定义的属性),javascript,node.js,webpack,pug,webpack-html-loader,Javascript,Node.js,Webpack,Pug,Webpack Html Loader,根据的自述文件,加载程序接受一个数据对象,该对象作为选项的一部分传递给模板。虽然在中的选项中不可用,但我发现的一个grunt插件()也以同样的方式使用API 我为加载程序指定了以下选项: loader: 'pug-html-loader', options: { pretty: true, exports: false, debug: !env.production, compileDebug: !env.production, cache: co
数据
对象,该对象作为选项
的一部分传递给模板。虽然在中的选项中不可用,但我发现的一个grunt插件()也以同样的方式使用API
我为加载程序指定了以下选项:
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
}
}
我想通过以下方式访问,根据:
但是,我在编译时总是遇到以下错误:
ERROR in Error: Child compilation failed:
Module build failed: TypeError: Cannot read property 'title' of undefined
出于调试目的,我在模板中包含了以下行:
-
console.log(' DATA! ' + data);
其结果是:
DATA! undefined
我还通过声明一些胡言乱语而不是对象(例如字符串、纯json、数字…)来确保数据正确地传递给了帕格,然后帕格编译器会抱怨数据的格式无效等
任何面临相同问题的人?您应该直接引用数据(即不在字段前面加数据
)
因此,您应该这样更改代码:
{
loader: 'pug-html-loader',
options: {
data: {
title: 'Hello World'
}
}
}
然后从你的哈巴狗模板中引用它
doctype html
html
head
title= title
你是说title=title
还是data:{hello:'hello World'}
你确实是对的
doctype html
html
head
title= title