2016-11-11 97 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