可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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
}
}
}
回答2:
Here's a solution which worked for me
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.
回答3:
I used combined inject 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
回答4:
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;
<template>
<v-btn v-if="facebookSdkReady" color="primary" dark type="button" @click="login">
<v-layout mr-2>
<v-icon name="brands/facebook"/>
</v-layout>Login with Facebook
</v-btn>
</template>
<script>
export default {
name: 'LoginWithFacebook',
data() {
return {
facebookSdkReady: false
}
},
mounted() {
const installFacebookSdkScript = (d, s, id) => {
if (d.getElementById(id)) {
this.facebookSdkReady = true
return
}
let fjs = d.getElementsByTagName(s)[0]
let js = d.createElement(s)
js.id = id
js.src = 'https://connect.facebook.net/en_US/sdk.js'
fjs.parentNode.insertBefore(js, fjs)
}
installFacebookSdkScript(document, 'script', 'facebook-jssdk')
window.fbAsyncInit = () => {
FB.init({
appId: '16653216336xxxxx',
cookie: true,
xfbml: true,
version: 'v3.2'
})
FB.AppEvents.logPageView()
this.facebookSdkReady = true
}
},
methods: {
// just a sample code copied from my code base.
// here, you can replace this with your own.
async login() {
const url = await this.$store.dispatch('auth/fetchSocialLoginUrl', {
provider: 'facebook'
})
window.location.href = url
}
}
}
</script>