如何在Nuxt.js中使用Facebook SDK? [英] How to use Facebook SDK with Nuxt.js?
本文介绍了如何在Nuxt.js中使用Facebook SDK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您可以看到我的代码.
npm install vue init nuxt/koa my-project(koa @ 2)
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>
但是
sdk.js:96未捕获的TypeError:vm.statusChangeCallback不是函数
sdk.js:96 Uncaught TypeError: vm.statusChangeCallback is not a function
在使用Nuxt项目时( nuxt/koa ),什么是使用Facebook SDK?
When using the Nuxt project (nuxt/koa), what is the best way to use the Facebook SDK?
推荐答案
我今天遇到了同样的问题.这是我使用nuxt.js的解决方案
I running into the same problem this day. Here is my solution with nuxt.js
首先创建一个插件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'
})
我们可以使用Vue.FB
来调用FB SDK中的方法,并在任何这样的组件中监听fb-sdk-ready
事件:
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
}
}
}
这篇关于如何在Nuxt.js中使用Facebook SDK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文