我怎样才能包含mustache js

发布于 2025-01-09 14:50:29 字数 2007 浏览 0 评论 0原文

好的,所以我添加了类型模块,错误消失了,但我仍然无法使用 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:

error

and here you can see that nothing is displayed:

data displayed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§普罗旺斯的薰衣草 2025-01-16 14:50:29

您尝试加载的文件包含 ECMAScript 模块

要使用它,您需要重写代码,使其也是一个模块。

本质上,这意味着使用 type="module" 将代码移动到脚本元素中。

<script src="/js/mymodule.js" type="module"></script>

然后,在顶级模块内,您可以使用 导入语句

import mustache from "https://cdnjs.cloudflare.com/ajax/libs/mustache.js/4.2.0/mustache.min.js"

/* mustache is now a variable with the value imported from the module */

const view = {
  title: "Joe",
  calc: function () {
    return 2 + 4;
  }
};

const output = mustache.render("{{title}} spends {{calc}}", view);

console.log(output);

您也可以内联它。现场演示。

<script type="module">
    import mustache from "https://cdnjs.cloudflare.com/ajax/libs/mustache.js/4.2.0/mustache.min.js"

    const view = {
      title: "Joe",
      calc: function () {
        return 2 + 4;
      }
    };
   
    const output = mustache.render("{{title}} spends {{calc}}", view);

    console.log(output);
</script>

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".

<script src="/js/mymodule.js" type="module"></script>

Then, inside your top level module you can load dependencies using the import statement.

import mustache from "https://cdnjs.cloudflare.com/ajax/libs/mustache.js/4.2.0/mustache.min.js"

/* mustache is now a variable with the value imported from the module */

const view = {
  title: "Joe",
  calc: function () {
    return 2 + 4;
  }
};

const output = mustache.render("{{title}} spends {{calc}}", view);

console.log(output);

You can also inline it. Live demo under the fold.

<script type="module">
    import mustache from "https://cdnjs.cloudflare.com/ajax/libs/mustache.js/4.2.0/mustache.min.js"

    const view = {
      title: "Joe",
      calc: function () {
        return 2 + 4;
      }
    };
   
    const output = mustache.render("{{title}} spends {{calc}}", view);

    console.log(output);
</script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文