handlebars not working in production after link click

孤人 提交于 2019-12-13 10:32:10

问题


I am using handlebars and it is working fine in development. When I upload to github-pages, The handlebars template still works if i open directly to the page. But if I go to a page in a link, the handlebars loop doesn't work. Here is my code:

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <script src="../bower_components/handlebars/handlebars.min.js"></script>
</head>
<div id="content-placeholder"></div> 
<script id="some-template" type="text/x-handlebars-template">
  {{#each this}}
    <div class=" col-md-4 col-sm-6 " data-animation-type="fadeIn" data-animation-delay="0.5s" data-animation-duration="2s">
       <div class="item-wrap">
          <figure class="">
             <div class="popup-call">
                <a href="assets/custom/images/blog/01.jpg" class="gallery-item"><i class="flaticon-arrows-4"></i></a>
             </div>
             <img src="{{this.picture.picture.url}}" class="img-responsive" style="height: 200px" alt="img11"/>
             <figcaption>
                <div class="post-header">
                   <h5><a href="blogpost.html">{{this.name}}</a></h5>
                </div>

             </figcaption>
          </figure>
       </div>
    </div>
  {{/each}}
</script>
<script>
    $(document).ready(function(){
      var source   = $("#some-template").html();
      var template = Handlebars.compile(source);
      $.getJSON('https://my-url.com/teams.json', function(data){
        console.log(data);
        $("#content-placeholder").html(template(data));
      });
    });

</script>

I've tried doing it without the document.ready() but that doesn't change anything.


回答1:


It's fairly clear that the handlebars.js isn't loading, because of the relative path in the script tag:

 <script src="../bower_components/handlebars/handlebars.min.js"></script>

Replace src="" with your favorite CDN location:

For example

<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.6/handlebars.min.js"></script>


来源:https://stackoverflow.com/questions/41002754/handlebars-not-working-in-production-after-link-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!