【问题标题】:AddThis with Vue shares wrong linksAddThis 与 Vue 共享错误的链接
【发布时间】:2018-06-28 19:43:44
【问题描述】:

所以我有一个使用 AddThis 进行链接共享和 Vue 进行渲染的页面。现在,该页面显示包含多个页面的链接列表。在第一页上,所有共享都按预期工作,但在第二页上,它使用了第一页中的链接。

重现此的示例:

new Vue({
  el: '#app',
  data: {
    pages: [
      [{
        url: 'http://google.com',
        title: 'Google'
      }],
      [{
        url: 'http://yahoo.com',
        title: 'Yahoo'
      }]
    ],
    currentPage: 0
  },
  computed: {
    items() {
      return this.pages[this.currentPage]
    }
  },
  methods: {
    switchPage() {
      if (this.currentPage === 0) {
        this.currentPage = 1;
      } else {
        this.currentPage = 0;
      }
    }
  }
})
<script src="https://unpkg.com/vue"></script>

<div id="app">
  <div class="sharer" v-for="item in items">
    <div class="addthis_inline_share_toolbox" :data-url="item.url" :data-title="item.title">{{ item.title }}</div>
  </div>
  <button @click="switchPage">Switch pages</button>
</div>

<script src="https://s7.addthis.com/js/300/addthis_widget.js#pubid=ra-57f620a4f185d54b"></script>

在首页时,AddThis 正确地共享了 Google 主页。但是在第二页时,它没有选择data-* 共享雅虎的标签。

注意:您不能在此处真正运行 StackOverflow sn-p,因为 AddThis 想要访问沙盒 iframe 禁止的 cookie。运行版本也可以在https://jsfiddle.net/d1az3qb3/3/ 找到。请记住禁用广告拦截器以让 AddThis 加载。

我已经尝试过的

  • 切换页面后运行addthis.layers.refresh()
  • 正在运行addthis.toolbox('.addthis_inline_share_toolbox')

(直接和this.$nextTick(() =&gt; …)

问题

AddThis 是不是刚刚坏掉了,它与 Vue 不兼容还是有办法让它工作?

【问题讨论】:

    标签: javascript vue.js addthis


    【解决方案1】:

    我猜AddThis有问题我建议你渲染所有链接并使用v-show隐藏你不感兴趣的链接。

    new Vue({
      el: '#app',
      data: {
        pages: [
          {
            url: 'http://google.com',
            title: 'Google',
            id: 1
          },
          {
            url: 'http://yahoo.com',
            title: 'Yahoo',
            id: 2
          }
        ],
        currentPage: 0
      },
      computed: {
        selectedItem() {
          return this.pages[this.currentPage].id
        }
      },
      methods: {
        switchPage() {
          if (this.currentPage === 0) {
            this.currentPage = 1;
          } else {
            this.currentPage = 0;
          }
        }
      }
    })
    <script src="https://unpkg.com/vue"></script>
    
    <div id="app">
      <div class="sharer" v-for="item in pages">
        <div v-show="item.id === selectedItem" class="addthis_inline_share_toolbox" :data-url="item.url" :data-title="item.title">{{ item.title }}</div>
      </div>
      <button @click="switchPage">Switch pages</button>
    </div>
    
    <script src="https://s7.addthis.com/js/300/addthis_widget.js#pubid=ra-57f620a4f185d54b"></script>

    【讨论】:

    • 据我所知,这需要在初始化期间呈现所有链接,并且如果动态加载某些项目将无法正常工作,对吗?
    • 动态改变 pages 数组对 vuejs 来说不是问题。只要您使用 push/slice/... 之类的方法,该数组就是反应式的。如果这不起作用,那么问题可能是由于 AddThis。试试看,让我们知道。如果需要,请更新您的问题。
    • 是的,问题出在 AddThis 上。如果数组发生变化,它们只记住位置并取旧数据而不是新数据。
    【解决方案2】:

    因此,经过数小时的研究,在销毁并重新初始化所有 AddThis 层(使用 SmartLayer API)后,它似乎可以工作,如下所示:

    new Vue({
      el: '#app',
      data: {
        pages: [
          [{
            url: 'http://google.com',
            title: 'Google'
          }],
          [{
            url: 'http://yahoo.com',
            title: 'Yahoo'
          }]
        ],
        currentPage: 0
      },
      computed: {
        items() {
          return this.pages[this.currentPage]
        }
      },
      methods: {
        switchPage() {
          if (this.currentPage === 0) {
            this.currentPage = 1;
          } else {
            this.currentPage = 0;
          }
    
          this.$nextTick(() => {
            // The interesting bit:
            const destroyLayers = layers => layers.destroy();
            destroyLayers.pi = true;  // Somehow needed by AddThis
    
            addthis.layers(destroyLayers);
            addthis.layers.refresh();
          });
        }
      }
    })
    <script src="https://unpkg.com/vue"></script>
    
    <div id="app">
      <div class="sharer" v-for="item in items">
        <div class="addthis_inline_share_toolbox_ctwk" :data-url="item.url" :data-title="item.title">{{ item.title }}</div>
      </div>
      <button @click="switchPage">Switch pages</button>
    </div>
    
    <script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-57f620a4f185d54b"></script>

    有关功能齐全的 JSFiddle,请参阅 https://jsfiddle.net/msiemens/82ysztk9/1/

    【讨论】:

    • 这不起作用(包括 jsfiddle),可能 addthis 做了一些更改。
    • 在这方面花了几个小时。除了添加上面代码的“有趣的位”之外,没有其他任何工作。只有一个问题。整个屏幕在使用时会产生一个小凹凸。 :(
    【解决方案3】:

    我对刷新页面上所有 addThis 元素的解决方案并不满意。对于任何正在寻找干净解决方案的人,这里有一个 API 用于构建您自己的 addThis 按钮 https://www.addthis.com/academy/addthis-core-share-follow-api/

    你可以用类似的东西来实现它。

    <script>
      export default {
        methods: {
          bindAddThis() {
            this.$nextTick(() => {
              window.addthis.share({
                container_selector: ".content--sharing-icons",
                button_selector: ".addthis_share_button"
              });
            });
          }
        },
        afterUpdate() {
          this.bindAddThis();
        },
        mounted() {
          this.bindAddThis();
        }
      };
    </script>
    

    遗憾的是 addThis API 不将元素作为选择器,因此您不能使用 $refs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-15
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多