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?
I running into the same problem this day. Here is my solution with nuxt.js
First create a plugin
plugins/fb-sdk.js
We can use
Vue.FB
to invoke methods in FB SDK and listen to thefb-sdk-ready
event in any component like this:I used combined inject to insert the plugin in the context (thus making it accessible in the store):
In
plugins/fb-sdk.js
:Making it accessible through
this.$fb
A plugin is a good way to inject some feature related logic into Vue instance but, I don't think your Vue instance has to have Facebook login logic all the time. I'd rather go with a
login-with-facebook-button
component like the following fashion;Here's a solution which worked for me
First create a javascript file and include it in your static/js folder:
include the script below in your fb-sdk.js file:
Finally in your nuxt.config.js, include the script at the header section:
You can now use
window.FB
to execute methods available in FB SDK in your components.