Meteor 为什么不';是否执行了t脚本标记?

Meteor 为什么不';是否执行了t脚本标记?,meteor,Meteor,我试图将Google Plus一个按钮包含在一个模板中,我注意到模板中的脚本标记没有执行 <template name="gplus"> <!-- Place this tag where you want the +1 button to render. --> <div class="g-plusone" data-href="{{url}}"></div> <!-- Place this tag after t

我试图将Google Plus一个按钮包含在一个模板中,我注意到模板中的
脚本
标记没有执行

<template name="gplus">
    <!-- Place this tag where you want the +1 button to render. -->
    <div class="g-plusone" data-href="{{url}}"></div>

    <!-- Place this tag after the last +1 button tag. -->
    <script type="text/javascript">
        console.log("Google Plus button");
        (function() {
            var po = document.createElement('script');
            po.type = 'text/javascript';
            po.async = true;
            po.src = 'https://apis.google.com/js/plusone.js';
            var s = document.getElementsByTagName('script')[0];
            s.parentNode.insertBefore(po, s);
        })();
    </script>
</template>

log(“Google Plus按钮”);
(功能(){
var po=document.createElement('script');
po.type='text/javascript';
po.async=true;
po.src=https://apis.google.com/js/plusone.js';
var s=document.getElementsByTagName('script')[0];
s、 parentNode.insertBefore(po,s);
})();
我通过将脚本移动到一个单独的JavaScript文件来实现这个按钮,但我仍然想知道为什么内联脚本不起作用


我在控制台上看到了脚本标记,但脚本本身没有运行。Meteor是如何做到这一点的?

Meteor只是将gplus模板的内容插入DOM中,因此当然不会发生任何事情,因为在DOM中添加元素时不会执行脚本


要解决此问题,您可以创建一个.js文件并将其放入客户端文件夹,Meteor将自动为您包括并执行该文件(以及在生产中缩小)。

以下是我在应用程序中加载Google+1按钮的操作:

1.在应用程序的
标记之间添加JS库。
我的印象是,插入DOM的脚本会在插入时由浏览器自动执行。不是吗?你能为进一步阅读这个主题提供一些建议吗?Meteor除了在DOM上执行一个基本的脚本节点附加操作之外,还做了一些其他的事情。模板文件被转换成Javascript字符串,然后在不同的函数范围内执行,以保持反应性。因此,不能依赖于正常的脚本插入,这就是为什么提供了呈现事件处理程序,以便您可以在插入后执行脚本(例如)。在您的情况下,您可能希望您的plusone代码在页面上可用,因此我建议将其分离到自己的文件中。谢谢!你知道它是怎么做到的吗?我的意思是涉及到客户端JavaScript?Meteor有一个名为Spark的模板系统。您可以在此处阅读源代码,了解它如何解析.html模板并处理它们:
<head>
  <!-- Load JS Library -->
  <script src="https://apis.google.com/js/platform.js" async="async" defer="defer"></script>
</head>
<template name="myTemplate">
  <div data-href="https://hackisition.com/" data-size="medium" class="g-plusone"></div>
</template>
Template.myTemplate.rendered = function() {
  return gapi.plusone.go();
};