我想知道如何分离淘汰模板文件并在我的项目中使用它。 我的项目运行正常,但是当我将模板放在外部文件中时,它出错了。
我不知道的一件事,模板扩展应该是什么?
我使用以下相同的代码制作了两个模板,分别为 person-template.js 和 person-template.js:
<script type="text/html" id="person-template">
<h3 data-bind="text: name"></h3>
<p>Credits: <span data-bind="text: credits"></span></p>
</script>
和我的页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type='text/javascript' src='knockout-3.5.1.js'></script>
<title>Document</title>
</head>
<body>
<h2>Participants</h2>
Here are the participants:
<div data-bind="template: { name: 'person-template', templateUrl: '.', foreach: people }"></div>
<script type="text/javascript">
function MyViewModel() {
this.people = [
{ name: 'Franklin', credits: 250 },
{ name: 'Mario', credits: 5800 }
]
}
ko.applyBindings(new MyViewModel());
</script>
</body>
</html>
我遇到了这个错误
<块引用>未捕获的错误:无法处理绑定“template: function(){return { name:'person-template',templateUrl:'.',foreach:people} }” 消息:找不到 ID 为 person-template 的模板 在 a.ba.a.ca.makeTemplateSource (knockout-3.5.1.js:118)