2017-05-31 123 views
1

根據自述文件pug-html-loader,加載程序接受data對象,該對象正在作爲options的一部分傳遞給模板。雖然optionspug 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,一個數字......),那麼帕格編譯器會抱怨數據不是有效格式等。

任何人都面臨同樣的問題?

回答

1

您應該引用數據directy(即無前綴與data領域)

所以,你應該改變你的代碼如下所示:

{ 
    loader: 'pug-html-loader', 
    options: { 
    data: { 
     title: 'Hello World' 
    } 
    } 
} 

然後從你的哈巴狗模板引用它

doctype html 
html 
    head 
    title= hello