【问题标题】:Mustache is not defined error in node js : Uncaught error节点js中的胡子未定义错误:未捕获的错误
【发布时间】:2019-03-30 04:04:51
【问题描述】:

我想使用Mustache.js 在客户端呈现页面,但它说未定义 Mustache。我找到了答案here,但我刚开始学习网站/渲染等,这很难理解。为了学习,我需要解决这个问题,但我不知道我该怎么做。

由于我是scripting languages 的初学者,有人可以告诉我我需要做什么来修复它,而无需详细说明。

这是我的代码:我正在尝试打开一个新页面,然后简单地呈现它。

socket.on('sign-up-succes',(data) => {
  window.open(data.redirect,"_self");
  var template = jQuery('#message-template').html();
  var html = Mustache.render(template,{
    text : "Hello"
   });
  jQuery('#messages').append(html);
});

【问题讨论】:

  • 嗨,汗,您是否使用 mustache 包定义了第一个 Mustache? var Mustache = require('mustache'),我也想知道有没有安装(npm i mustache
  • @JorgeValentini 是的,我做到了,它已定义,但无法通过 Mustache.... 访问。
  • 你能把你定义的那行贴出来吗?要查看失败的完整 sn-p 并重现它?
  • 能否贴出更大的代码sn-p,很难理解你的问题。

标签: javascript node.js rendering render mustache


【解决方案1】:

从您发布的代码中,我看到您正在使用 window 对象,所以我相信您正在尝试在客户端脚本中使用 Mustache

如果是,那么使用下面的CDN:

https://cdnjs.cloudflare.com/ajax/libs/mustache.js/3.0.0/mustache.js

https://cdnjs.cloudflare.com/ajax/libs/mustache.js/3.0.0/mustache.min.js

我建议您从服务器端渲染模板(在您的情况下为 Node.js)。

【讨论】:

    【解决方案2】:

    jsFiddle 演示 - http://jsfiddle.net/cvqLkb4t/

    HTML

    <script src="https://raw.githubusercontent.com/janl/mustache.js/master/mustache.min.js" type="application/javascript"></script>
        <script>
        socket.on('sign-up-succes',(data) => {
          window.open(data.redirect,"_self");
          var template = jQuery('#message-template').html();
          var html = Mustache.render(template,{
            text : "Hello"
           });
          jQuery('#messages').append(html);
        });
        </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-14
      • 2013-10-27
      • 1970-01-01
      • 1970-01-01
      • 2020-02-02
      • 2020-07-02
      • 1970-01-01
      相关资源
      最近更新 更多