我怎样才能包含mustache js
好的,所以我添加了类型模块,错误消失了,但我仍然无法使用 Mustache,也许我使用错误,我什至尝试在本地包含它,所以在这里你可以看到我的代码也许你可以帮助我,所以没有小胡子(完美工作):
HTML 端
<h2>Mustache Test</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Distinctio sint pariatur soluta nemo odit dolor ipsum laboriosam dolore ullam rerum commodi, vitae culpa totam autem praesentium, iste eveniet accusantium nam?</p>
<ul id="someData"></ul>
Javascript 端:
$.ajax({
type: "GET",
url: "http://localhost:8000/api/displayAllUsers",
success: function (data) {
$.each(data, function (i, user) {
$("#someData").append('<li> name: '+ user.name +' </li>');
});
},
error: function () {
console.log("Fail");
},
});
所以这工作完美: 没有 Mustache 的结果
但我想使用 Mustache,因为将来我需要更复杂的模板所以这就是我尝试过的:
Html 端:
<ul id="someData">
<template id ="user-template">
<li>
<p>
<strong>Name:</strong>
<span>{{name}}</span>
</p>
</li>
</template>
</ul>
Javascript 端:
$(document).ready(function () {
var userTemplate = $("user-template").html();
function displayUsers(user){
$("#someData").append(Mustache.render(userTemplate, user));
}
$.ajax({
type: "GET",
url: "http://localhost:8000/api/displayAllUsers",
success: function (data) {
$.each(data, function (i, user) {
displayUsers(user);
});
},
error: function () {
console.log("Fail");
},
});
});
所以我不认为我在这里错了,因为在我的浏览器控制台中它说 Mustache 未定义,这是错误:
,在这里您可以看到没有显示任何内容:
Ok so i added the type module and the error is gone but i still cant use Mustache, maybe i'm using it wrong i even tried to include it locally, so here you can see my code maybe you can help me with it, so without Mustache (working perfectly) :
The HTML side
<h2>Mustache Test</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Distinctio sint pariatur soluta nemo odit dolor ipsum laboriosam dolore ullam rerum commodi, vitae culpa totam autem praesentium, iste eveniet accusantium nam?</p>
<ul id="someData"></ul>
Javascript side :
$.ajax({
type: "GET",
url: "http://localhost:8000/api/displayAllUsers",
success: function (data) {
$.each(data, function (i, user) {
$("#someData").append('<li> name: '+ user.name +' </li>');
});
},
error: function () {
console.log("Fail");
},
});
so this is working perfectly :
results without Mustache
But i want to work with Mustache because i will need more complicated template in the future so here's what i tried:
Html Side:
<ul id="someData">
<template id ="user-template">
<li>
<p>
<strong>Name:</strong>
<span>{{name}}</span>
</p>
</li>
</template>
</ul>
Javascript side:
$(document).ready(function () {
var userTemplate = $("user-template").html();
function displayUsers(user){
$("#someData").append(Mustache.render(userTemplate, user));
}
$.ajax({
type: "GET",
url: "http://localhost:8000/api/displayAllUsers",
success: function (data) {
$.each(data, function (i, user) {
displayUsers(user);
});
},
error: function () {
console.log("Fail");
},
});
});
So i dont think I am wrong here, because in my browser console it saying that Mustache is not defined, here is the error:
and here you can see that nothing is displayed:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试加载的文件包含 ECMAScript 模块。
要使用它,您需要重写代码,使其也是一个模块。
本质上,这意味着使用
type="module"
将代码移动到脚本元素中。然后,在顶级模块内,您可以使用
导入
语句。您也可以内联它。现场演示。
The file you are trying to load contains an ECMAScript module.
To use it you need to rewrite your code so that it is also a module.
Essentially that means moving your code into a script element with
type="module"
.Then, inside your top level module you can load dependencies using the
import
statement.You can also inline it. Live demo under the fold.