4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Nuxt.js【Vue.js】でシェアボタン(ページ共有)を実装する

Last updated at Posted at 2021-07-19

Nuxt.js【Vue.js】でシェアボタン(ページ共有)を実装する方法になります。

Nuxt.js【Vue.js】でシェアボタン(ページ共有)を実装する方法

<template>
  <div class="share_buttons">
    <button v-if="isNavigatorShareButton" @click="navigatorShare">
      <font-awesome-icon icon="share-alt" />
    </button>
    <button v-if="!isNavigatorShareButton" @click="twitterShare">
      <font-awesome-icon :icon="['fab', 'twitter']" />
    </button>
    <button v-if="!isNavigatorShareButton" @click="facebookShare">
      <font-awesome-icon :icon="['fab', 'facebook']" />
    </button>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'

export default Vue.extend({
  data() {
    return {
      isNavigatorShareButton: true,
      title: 'タイトル',
      text: 'テキストが入ります',
      url: 'https://demo.jp/demo'
    }
  },
  mounted() {
    if (navigator.share === undefined) {
      this.isNavigatorShareButton = false
    }
  },
  methods: {
    // Web Share APIが使える場合
    navigatorShare() {
      if (navigator.share) {
        navigator.share({
          title: this.title,
          text: this.title + '\n' + this.text,
          url: this.url
        })
      }
    },
    // Twitter
    twitterShare() {
      const baseUrl = 'https://twitter.com/intent/tweet?'
      const text = ['text', this.title + '\n' + this.text]
      const url = ['url', this.url]
      const parameter = new URLSearchParams([text, url]).toString()
      const shareUrl = `${baseUrl}${parameter}`
      window.open(shareUrl, 'twitter', 'top=200,left=300,width=600,height=400')
    },
    // Facebook
    facebookShare() {
      const baseUrl = 'https://www.facebook.com/sharer/sharer.php?'
      const url = ['u', this.url]
      const parameter = new URLSearchParams([url]).toString()
      const shareUrl = `${baseUrl}${parameter}`
      window.open(shareUrl, 'facebook', 'top=200,left=300,width=600,height=400')
    }
  }
})
</script>

シェアボタンについては、Font Awesomeを使用しています。

解説

Web Share APIが使える場合

共有に便利なWeb Share APIが使える場合はそちらを利用します。

navigator.PNG
上記の画像のように、任意の宛先に共有することが可能です。

if (navigator.share) {
  navigator.share({
    title: this.title,
    text: this.title + '\n' + this.text,
    url: this.url
  })

navigator.shareは、title、text、urlの3つのオプションを設定することができます。

ボタンについては、mountedでWeb Share APIが使えるかを判定して、表示を切り替えています。

mounted() {
  if (navigator.share === undefined) {
    this.isNavigatorShareButton = false
  }
}

Web Share APIが使えない場合

Web Share APIが使えない場合は、TwitterとFacebookのシェアボタンを設置しています。

window.open()でリンク先を別タブで開くようにしています。

Twitter

twitter.jpg

twitterShare() {
  const baseUrl = 'https://twitter.com/intent/tweet?'
  const text = ['text', this.title + '\n' + this.text]
  const url = ['url', this.url]
  const parameter = new URLSearchParams([text, url]).toString()
  const shareUrl = `${baseUrl}${parameter}`
  window.open(shareUrl, 'twitter', 'top=200,left=300,width=600,height=400')
}

Facebook

facebook.jpg

faceBookShare() {
  const baseUrl = 'https://www.facebook.com/sharer/sharer.php?'
  const url = ['u', this.url]
  const parameter = new URLSearchParams([url]).toString()
  const shareUrl = `${baseUrl}${parameter}`
  window.open(shareUrl, 'facebook', 'top=200,left=300,width=600,height=400')
}
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?