使用webpack从html文件加载knockout模板

时间:2017-03-23 00:12:26

标签: typescript knockout.js webpack webpack-2

我正在尝试从外部文件加载我的淘汰模板 我的设置如下:

  • NPM
  • 的WebPack
  • 打字稿

现在,我尝试了一些不同的选择但没有效果:

text-loader / raw-loader / html-loader

template: require("text-loader!./my-component.html")
// or
template: require("raw-loader!./my-component.html")
// or
template: require("html-loader!./my-component.html")

没有工作,因为这会返回一个类似Javascript代码的文本,里面有我的模板。

knockout-template-loader

template: require("knockout-template-loader!html-loader!./my-component.html")

没有工作因为它只返回一个空对象。

script-template-loader

template: require("script-template-loader?addToDom=true!./hello.html")

这有两个问题:

  1. 模板不像HTMLScriptElement对象
  2. 生成的脚本在字符串[object Object]
  3. 旁边没有实际内容

    require选项

    template: {require: "text-loader!./my-component.html"}
    

    我没有工作,因为我在运行时收到错误:

      

    无法阅读财产'来电'未定义__webpack_require__

    我缺少什么?

1 个答案:

答案 0 :(得分:2)

html-loader插件确实有效。为什么它最初没有工作的是我在webpack.config.js中注册了knockout-template-loader作为html文件的默认加载器。
即使在require中指定了特定的加载器,它看起来也在使用它 删除该规则后,它现在可以使用