2016-11-11 55 views
0

由於webpack 2引入了ES6模塊系統,因此require('./mytemplate.html')語法在基於角1和webpack 2的項目中不起作用。Webpack 2 Angular 1模板加載器?

無法找到與webpack 2一起使用來加載html模板的加載程序。

什麼是使用webpack2加載角1模板文件的正確方法?

注意:我正在使用typescript類創建角1組件,它具有template:require('./ mytemplate.html')屬性。舉個例子。

export class MyComponent implements ng.IComponentOptions { 
    public template: string = require('./mytempalte.html'); 
    public controller = MyControllerClass; 
} 
+0

我可能是錯的,但我想可以使用相同的加載器,而不是將模板內容分配給某個對象屬性嘗試導入到一個變量中,該變量將分配給對象屬性。就像從'。template.html''導入模板一樣。然後'someObject.template = template;'。 – k10der

+0

我嘗試從'./mytemplate.html'中導入: 導入模板;但由於無法識別html模板而導致錯誤。我更新了問題,提到我也使用打字稿。 – Nexus23

+0

看起來,他們最近添加了這個功能(https://github.com/webpack/html-loader/pull/97),但他們還沒有在npm中進行更新。所以我認爲你要麼必須等待,要麼有你自己的本地構建版本的webpack加載器。 – k10der

回答

0

到目前爲止,webpack 2仍然支持require語法。什麼工作對我來說是安裝節點分型:

@types/node 

這消除了需要的時候,我有錯誤tempaltes e.g

export class AdminComponent implements ng.IComponentOptions { 
    public template: string = require('./admin.component.html'); 
    public controller = AdminComponentController; 
} 

仍在使用HTML的加載器加載,雖然這些模板。

+0

是的,但不重寫你現有的代碼,你可以嘗試任何加載器(只是自己試着不確定它會工作)https://github.com/WearyMonkey/ngtemplate-loader,它會爲你處理這些字符串(所以你將不需要在你的templateUrl中添加require或甚至ngtemplate!)。 – Rantiev