我试图通过触发click事件来使用角度为6的facebook api。我了解window.fbAsyncInit首先加载sdk,然后在其中加载可以自动运行的FB.login
但是我想在window.fbAsyncInit之后从组件html调用一个函数。
indx.html,它将在我不需要的负载下自动调用fb登录
window.fbAsyncInit = function() {
FB.init({
appId : 'my-app-id',
cookie : true,
xfbml : true,
version : 'v3.1'
});
FB.AppEvents.logPageView();
//i can do this instead that will call fb login automatically, bt i want a click event from component.html file
FB.login(function(response) {
console.log(response); //here i am getting response automatially that i dont want, i want to fire a click event
});
}
(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'));
component.html在这里,我想从函数内的click事件调用fb.login
<button type="button" class="btn btn-facebook" (click) = "facebookLogin();">Facebook Login</button>
component.ts调用此函数,但在index.html中的fbAsyncInit之后没有初始化
facebookLogin(){
FB.login((response)=>
{
console.log('submitLogin',response);
if (response.authResponse)
{
//login success
//login success code here
//redirect to home page
}else{
console.log('User login failed');
}
});
}
我想在index.html中的fbAsyncinit内单击时调用此函数,而不会出现该错误。你能建议吗
答案 0 :(得分:-1)
导入后声明。
<.. imports ...>
声明var FB;
@Component({ 选择器:“ app-home”, ... })