6

共同スケッチ用に Google Plus One ボタンをMeteor アプリに含めようとしたところ、scriptテンプレート内のタグが実行されていないことに気付きました。

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

スクリプトを別の JavaScript ファイルに移動することでボタンが機能するようになりましたが、インライン スクリプトが機能しなかった理由はまだわかりません。

コンソールにスクリプト タグが表示されますが、スクリプト自体が実行されません。Meteor はどのようにこれを行いますか?

4

2 に答える 2

4

Meteor は gplus テンプレートのコンテンツを DOM に挿入するだけなので、要素が DOM に追加されるときにスクリプトが実行されないため、もちろん何も起こりません。

これを修正するには、.js ファイルを作成して client フォルダーに配置します。これは Meteor によって自動的に含まれて実行されます (本番環境では圧縮されます)。

于 2013-04-15T15:01:18.130 に答える
2

アプリケーションに Google +1 ボタンをロードするには、次のようにします。

<head>1.アプリケーションの タグの間に JS ライブラリを追加します。

<head>
  <!-- Load JS Library -->
  <script src="https://apis.google.com/js/platform.js" async="async" defer="defer"></script>
</head>

2. +1 ボタンを挿入します。

<template name="myTemplate">
  <div data-href="https://hackisition.com/" data-size="medium" class="g-plusone"></div>
</template>

3. ボタンをレンダリングします。

Template.myTemplate.rendered = function() {
  return gapi.plusone.go();
};
于 2014-08-22T09:22:28.447 に答える