Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Facebook SDK with Nuxt.js?

You can see my code.

npm install vue init nuxt/koa my-project (koa@2)

pages
|- login.vue

<script>
export default {
  name: 'login',
  method: {
    login () {
      let vm = this
      FB.login(function (response) {
        vm.statusChangeCallback(response)
      }, {scope: 'publish_actions'})
    }
  },
  mounted () {
    console.log('mounted')
    let vm = this
    window.fbAsyncInit = () => {
      FB.init({
        appId: 'my-facebook-app-id',
        cookie: true,
        xfbml: true,
        version: 'v2.8'
      })
      FB.getLoginStatus(function (response) {
        vm.statusChangeCallback(response)
      })
    }

    (function(d, s, id){
      var js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) {return;}
      js = d.createElement(s); js.id = id;
      js.src = "//connect.facebook.net/en_US/sdk.js";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
  }
}
</script>

but,

sdk.js:96 Uncaught TypeError: vm.statusChangeCallback is not a function

When using the Nuxt project (nuxt/koa), what is the best way to use the Facebook SDK?

like image 504
hyeokluv Avatar asked Apr 27 '17 18:04

hyeokluv


People also ask

Is Nuxt faster than Vue?

Nuxt offers better SEO improvement with its server-side rendering feature, faster development with auto-generic router, public share features and management with great configuration options and meta tags methods, automatic code splitting with pre-rendered pages – all of this is impossible or extremely complex to ...

Is Nuxt SEO friendly?

Nuxt, one of the most popular Vue frameworks for new web apps, can greatly improve your app performance and SEO.

Is Nuxt 3 ready for production?

Nuxt 3 is in release candidate stage and available as the nuxt@rc npm tag. The latest updates on Nuxt 2. x are available via the nuxt-edge npm package and are expected to be released soon for forward compatibility.

What is SSR in Nuxt?

Server-side rendering (SSR), is the ability of an application to contribute by displaying the web-page on the server instead of rendering it in the browser. Server-side sends a fully rendered page to the client; the client's JavaScript bundle takes over which then allows the Vue. js app to hydrate .


3 Answers

I running into the same problem this day. Here is my solution with nuxt.js

First create a plugin plugins/fb-sdk.js

const vue_fb = {}
vue_fb.install = function install(Vue, options) {
    (function(d, s, id){
        var js, fjs = d.getElementsByTagName(s)[0]
        if (d.getElementById(id)) {return}
        js = d.createElement(s)
        js.id = id
        js.src = "//connect.facebook.net/en_US/sdk.js"
        fjs.parentNode.insertBefore(js, fjs)
        console.log('setting fb sdk')
    }(document, 'script', 'facebook-jssdk'))

    window.fbAsyncInit = function onSDKInit() {
        FB.init(options)
        FB.AppEvents.logPageView()
        Vue.FB = FB
        window.dispatchEvent(new Event('fb-sdk-ready'))
    }
    Vue.FB = undefined
}

import Vue from 'vue'

Vue.use(vue_fb, {
    appId: 'your-app-id',
    autoLogAppEvents: true,
    xfbml: true,
    version: 'v2.9'
})

We can use Vue.FB to invoke methods in FB SDK and listen to the fb-sdk-ready event in any component like this:

export default{
    data: function () {
        return {isFBReady: false}
    },
    mounted: function () {
        this.isFBReady = Vue.FB != undefined
        window.addEventListener('fb-sdk-ready', this.onFBReady)
    },
    beforeDestroy: function () {
        window.removeEventListener('fb-sdk-ready', this.onFBReady)
    },
    methods: {
        onFBReady: function () {
          this.isFBReady = true
        }
    }
}
like image 193
ChaoTangChang Avatar answered Dec 14 '22 06:12

ChaoTangChang


First create a javascript file and include it in your static/js folder:

static/js/fb-sdk.js

include the script below in your fb-sdk.js file:

window.fbAsyncInit = function () {
  FB.init({
    appId: '<insert your app id>',
    autoLogAppEvents: true,
    xfbml: true,
    version: 'v2.10'
  })
  FB.AppEvents.logPageView()
};

(function (d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0]
  if (d.getElementById(id)) { return }
  js = d.createElement(s); js.id = id
  js.src = 'https://connect.facebook.net/en_US/sdk.js'
  fjs.parentNode.insertBefore(js, fjs)
}(document, 'script', 'facebook-jssdk'))

Finally in your nuxt.config.js, include the script at the header section:

module.exports = {
  head: {
    title: 'myTitle',
    meta: [
      { charset: 'utf-8' }
    ],
    script: [
     { src: '/js/fb-sdk.js' }
    ]
  }
}

You can now use window.FB to execute methods available in FB SDK in your components.

like image 40
MSyakir Avatar answered Dec 14 '22 06:12

MSyakir


I used [combined inject][1] to insert the plugin in the context (thus making it accessible in the store):

In plugins/fb-sdk.js:

const vue_fb = {}
vue_fb.install = function install(Vue, options) {
    (function(d, s, id){
        var js, fjs = d.getElementsByTagName(s)[0]
        if (d.getElementById(id)) {return}
        js = d.createElement(s)
        js.id = id
        js.src = "//connect.facebook.net/en_US/sdk.js"
        fjs.parentNode.insertBefore(js, fjs)
        console.log('setting fb sdk')
    }(document, 'script', 'facebook-jssdk'))

    window.fbAsyncInit = function onSDKInit() {
        FB.init(options)
        FB.AppEvents.logPageView()
        Vue.FB = FB
        window.dispatchEvent(new Event('fb-sdk-ready'))
        vue_fb.sdk = FB // do not forget this line
    }
    Vue.FB = undefined
}

import Vue from 'vue'

Vue.use(vue_fb, {
    appId: 'your-app-id',
    autoLogAppEvents: true,
    xfbml: true,
    version: 'v2.9'
})
// and this line
export default ({ app }, inject) => {
inject('fb', vue_fb)
}

Making it accessible through this.$fb [1]: https://nuxtjs.org/guide/plugins/

like image 39
WitoldW Avatar answered Dec 14 '22 04:12

WitoldW