使用Vue的AddThis共享错误的链接

时间:2018-01-19 14:26:04

标签: javascript vue.js addthis

所以我有一个页面使用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主页。但是当在第二页上时,它没有拿起用于共享Yahoo的data-*标签。

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

我已经尝试了什么

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

(直接和this.$nextTick(() => …)

问题

AddThis是否已损坏,是否与Vue不兼容或是否有办法使其正常工作?

3 个答案:

答案 0 :(得分:0)

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

&#13;
&#13;
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;
      }
    }
  }
})
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

所以,经过几个小时的挖掘,它似乎在破坏并重新初始化所有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/

答案 2 :(得分:0)

我对刷新页面上所有addThis元素的解决方案不满意。对于正在寻找干净解决方案的任何人,这里https://www.addthis.com/academy/addthis-core-share-follow-api/

都有一个用于构造自己的addThis按钮的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