【问题标题】:Disqus comments don't work in a polymer custom elementDisqus 注释在聚合物自定义元素中不起作用
【发布时间】:2016-09-28 21:06:42
【问题描述】:

我不知道如何让 disqus cmets 代码在我的自定义元素中工作。

我的网站结构:

| index.html
--------\my-app.html(自定义元素)
----------------\ my-testView1.html(自定义元素)
----------------\ my-testView2.html(自定义元素)

我需要将disqus cmets放入my-testView1.htmlmy-testView2.html

index.html的结构:

 <body>
   <my-app>
      <div class="disqusClass1" id="disqus_thread"></div>
      <div class="disqusClass2" id="disqus_thread"></div>
   <my-app>
</body>

my-app.html的结构:

 <iron-pages>
      <my-testView1 name="testView"><content select=".disqusClass1"></content></my-testView1>
      <my-testView2 name="testView2"><content select=".disqusClass2"></content></div></my-testView2>
    </iron-page‌​s>

my-testView1.html 的结构:

<template>
  <content select=".disqusClass1"></content>
</template>

my-testView2.html 的结构:

<template>
  <content select=".disqusClass2"></content>
</template>

disqus div

我将disqus cmets 的div 放在&lt;my-app&gt; 中的index.html 上,以便chrome 可以找到它。如果我像这样将它放在&lt;my-testView1&gt; 中,它找不到

页面my-app.html

<iron-pages>
  <my-testView1 name="testView"><div id="disqus_thread"></div></my-testView1>
  <my-testView2 name="testView2"><div id="disqus_thread"></div></my-testView2>
</iron-page‌​s>

因为 my-app.html 本身就是一个自定义元素,它不会让 chrome 找到它。所以我不得不把它放在 shadow dom 之外(index.html 页面)

my-testView1.htmlmy-testView2.html页面上的 Javacript 代码如下所示:

<dom-module id="my-testView1">
  <template>
   ...
        <content></content>
   ...
 </template>

  <script>
    Polymer({
      is: 'my-testView1',

      ready: function () 
          {    
             // DEFAULT DISQUS CODE (I changed real URLs though):        
             var disqus_config = function () {
             this.page.url = 'https://www.example.com/testView1'; // Replace PAGE_URL with your page's canonical URL variable
             this.page.identifier = '/testView1'; 
             // this.page.title = 'Test View';
             };

            (function() { 
            var d = document, s = d.createElement('script');
            s.src = '//myChannelName.disqus.com/embed.js';
            s.setAttribute('data-timestamp', +new Date());
            (d.head || d.body).appendChild(s);
            })();
        }
     });
  </script>
</dom-module>

结果

评论当时仅出现在其中一个my-testView1.html my-testView2.html 上。我需要 my-testView1.html 上的 1 个 disqus 线程和 my-testView2.html 上的另一个 disqus 线程

也许是因为路由。 Disqus 控制台消息说我需要使用 ajax 方法 https://help.disqus.com/customer/portal/articles/472107-using-disqus-on-ajax-sites 不幸的是,当我将上面的 javascript 代码替换为示例中的代码时,我无法使其工作:

  <script>
    Polymer({
      is: 'my-testView1',

      ready: function () 
          {    
                 var disqus_shortname = 'myDisqusChannelId';
                 var disqus_identifier = '/testView1';
                 var disqus_url = 'http://example.com/testView1';
                 var disqus_config = function () { 
                   this.language = "en";
                 };

                 (function() {
                     var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
                     dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
                     (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
                 })();

                 var reset = function (newIdentifier, newUrl) {
                     DISQUS.reset({
                         reload: true,
                         config: function () {
                             this.page.identifier = newIdentifier;
                             this.page.url = newUrl;
                         }
                     });
                 };
        }
     });
  </script>
</dom-module>

在两个自定义元素中(相应地更改 disqus_identifierdisqus_url

【问题讨论】:

  • 您可能不必将 Disqus 脚本放在主文档中。看看我如何将它放在元素的模板中并绑定channelName 属性:jsbin.com/feqaqig/edit?html,js,output
  • 现在想想,将 Disqus 放在 Polymer 元素中可能根本不是一个好主意。它在 Shadow DOM 下不起作用,我真的看不到任何好处
  • @Supersharp 我在问题中添加了 UPDATE #1,如果您有空闲时间,请查看一下吗?
  • @TomaszPluskiewicz 好吧,我将我网站的页面作为自定义元素,我想将 disqus 的script 放入其中一些页面(自定义元素)。但正如你所说,它不会在 shadow dom 中工作,而且我是新手,所以我还不知道如何实现它。不幸的是,聚合物文档中没有关于自定义聚合物元素内部脚本的有用(对于新手)信息
  • 每次显示视图(1 或 2)时都应调用 DISQUS.resest() 方法

标签: javascript polymer custom-element polymer-elements html5-template


【解决方案1】:

错误是由于 disqus 库找不到&lt;div id="disqus_thread"&gt; 元素造成的。

这是因为这个元素在 Shadow DOM 中(这就是为什么它在没有实现真正 Shadow DOM 的 Firefox 中运行良好的原因)。

3 种可能的解决方案:

  1. 不要在您的 Polymer 元素中使用 Shadow DOM。
  2. 不要将 #disqus_thread 元素放在 Polymer 元素中(将其插入到普通 DOM 中)。
  3. 在您的&lt;template&gt; 中使用&lt;content&gt;,并在聚合物标签内使用#disqus_thread 元素以使其可用于库:

在自定义元素中:

<template>
   //HTML code here
   <content></content>
</template>

在您插入自定义元素的 HTML 页面中:

<my-app>
   <my-testView>
      <div id="disqus_thread"></div>
   </my-testView>
</my-app>

&lt;div&gt; 将在放置(嵌套)&lt;content&gt; 标签的位置显示

【讨论】:

  • 感谢您的回答。我有这个结构index.html --/ my-app.html (custom element) --/ my-testView.html (custom element)。所以它是自定义元素内部的自定义元素。那么我基本上必须将&lt;div id="disqus_thread"&gt;&lt;/div&gt; 放入index.html &lt;body&gt; 中吗?我只是这样做了,我还将&lt;content&gt;&lt;/content&gt; 放到了我的my-testView.html 自定义元素页面上需要的位置。我现在可以在固定在页面顶部的所有页面上看到 disqus。但它没有转到&lt;content&gt; 标签。哦,伙计,成为网络开发的新手真的很糟糕..
  • 如果您有嵌套的自定义元素,则必须在所有自定义元素模板中添加内容元素。不要同时应用 2 个不同的解决方案。
  • 好的。我现在所做的只是将 &lt;div id="disqus_thread"&gt;&lt;/div&gt; 放在 index.html 上的 body 标记内,然后将 &lt;iron-pages&gt;&lt;my-testView name="testView"&gt;&lt;content&gt;&lt;/content&gt;&lt;/my-testView&gt;&lt;/iron-pages&gt; 内的 添加到自定义元素 my- app.html 我还添加了 到自定义元素 my-testView.html 在它需要的地方,但我就是不明白它怎么知道 index.html 上的 body 内的 &lt;div id="disqus_thread"&gt;&lt;/div&gt; 需要转到 my-testView.html 上的 &lt;content&gt;&lt;/content&gt; 我在这里做错了什么?
  • div 应该在您的自定义元素中,content 应该在template 中!如上例
  • 然后找不到它,因为my-app.html本身是自定义元素。我重写了我的问题并对其进行了更新,以便更容易看到嵌套的内容和未嵌套的内容,显示它的外观。我知道您可能已经厌倦了这个问题,但是如果您碰巧知道答案,如果您添加一些内容,我将不胜感激,非常感谢
【解决方案2】:

我想为在 Polymer 中使用 Disqus cmets 提供另一种可能的解决方案。主要问题是无法将 Disqus 与 shadow dom 中的元素一起使用,因为它们是隐藏的。那么我们如何才能让 shadow dom 元素可见呢?我们可以从应用程序的 index.html 向下传递组件层次结构。

要像这样在你的 html 中公开一个元素结构:

index.html
<polymer-app>
  <div id="disqus_thread">
</polymer-app>

在聚合物应用程序中:

<dom-module id="polymer-app">
  <template>
      <slot></slot>
  <template>
</dom-module>

插槽是显示#disqus_thread 的位置。您可以将其进一步向下传递给聚合物应用程序内部的其他组件。

<dom-module id="polymer-app">
  <template>
    <my-page>
      <slot></slot>
    </my-page>
  <template>
</dom-module>

注意:这是代码只是一个示例。不要试图复制和粘贴,它不会运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多