使用客户端模板的脚本标签,为什么不能通过src属性加载? [英] using script tags for client side templates, why can't i load via src attribute?

查看:97
本文介绍了使用客户端模板的脚本标签,为什么不能通过src属性加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这正常工作(可以通过id访问dom中的代码段)

this works fine (am able to access the snippet in the dom by id)

<script type="text/x-template" id="todo-item-template">
  <div class="todo-view">
    {blah} {blah}
  </div>
</script>

但是如果我将模板放在外部资源中,则无法在dom中找到它:

but if i put the template in an external resource, i can't find it in the dom:

<script type="text/x-template" id="todo-item-template" src="todo-item-template.html">

chrome给我一个警告,我认为这是无关紧要的:

chrome is giving me a warning which i think is irrelevant:

Resource interpreted as Script but transferred with MIME type text/html: 
"http://localhost:8000/todo2/todo-stats-template.html". 

我正在从文件系统和/或 python -m SimpleHTTPServer 运行.有什么想法吗?

i am running from the filesystem, and/or from python -m SimpleHTTPServer. any ideas?

推荐答案

这是因为服务器将 .html 文件作为HTML文件提供.您将希望模板文件具有不同的文件类型,例如 .tpl ,并让服务器发送适当的标头.

This is because the server is serving .html files as HTML files. You will want to have template files be a different file type, such as .tpl and have the server send the appropriate headers.

这篇关于使用客户端模板的脚本标签,为什么不能通过src属性加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆