amn*_*n41 9 reactjs google-signin
这与使用谷歌登录按钮的反应基本相同,但在我的情况下,接受的答案并没有解决它.
我有一个反应组件:
var LoginButton = React.createClass({
onSignIn: function(googleUser) {
console.error(" user signed in");
},
componentDidMount: function() {
console.log('component has mounted');
gapi.signin2.render('g-signin2', {
'scope': 'https://www.googleapis.com/auth/plus.login',
'width': 200,
'height': 200,
'longtitle': true,
'theme': 'dark',
'onsuccess': this.onSignIn
})
},
render: function() {
return (
<div className="g-signin2"/>
);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当登录成功运行时,永远不会调用onSignIn函数.实际上我没有指定用于渲染自定义登录按钮的选项(即方形和黑暗主题),所以我猜它因为<div>类的而呈现默认按钮.
我正在同步加载库,因为否则gapi在componentDidMount触发时不在范围内.
我看到的另一个选项是google建议<script src="https://apis.google.com/js/platform.js?onload=renderButton" async defer></script>在函数中使用和包装渲染.但是这个脚本必须加载到组件内部,否则我必须找到一种方法,这个函数在范围内作为回调传递index.html,这两者似乎都不是一个好的解决方案.
如何才能获得自定义登录按钮而不是默认登录按钮?
amn*_*n41 13
这个黑客现在可以做到这一点......
在我的index.htmlI中,当库加载时触发自定义事件:
<script src="https://apis.google.com/js/platform.js?onload=triggerGoogleLoaded" async defer></script>
<script type="text/javascript">
function triggerGoogleLoaded() {
window.dispatchEvent(new Event('google-loaded'));
}
</script>
Run Code Online (Sandbox Code Playgroud)
我的反应组件现在看起来像这样:
var LoginButton = React.createClass({
onSignIn: function(googleUser) {
console.log("user signed in"); // plus any other logic here
},
renderGoogleLoginButton: function() {
console.log('rendering google signin button')
gapi.signin2.render('my-signin2', {
'scope': 'https://www.googleapis.com/auth/plus.login',
'width': 200,
'height': 50,
'longtitle': true,
'theme': 'light',
'onsuccess': this.onSignIn
})
},
componentDidMount: function() {
window.addEventListener('google-loaded',this.renderGoogleLoginButton);
},
render: function() {
let displayText = "Sign in with Google";
return (
<div id="my-signin2"></div>
);
}
});
Run Code Online (Sandbox Code Playgroud)
请注意,渲染的<div>"my-signin2" id不是a class.
小智 6
我用过JQuery,通过componentDidMount方法加载脚本...
componentDidMount: function() {
$.getScript('https://apis.google.com/js/platform.js')
.done(() => {
// execute any gapi calls here...
gapi.signin2.render('g-signin2', {
// ... options here ...
});
});
},
Run Code Online (Sandbox Code Playgroud)
正如f13ts所说,呈现器<div>应使用“ my-signin2”作为ID,而不是类。
| 归档时间: |
|
| 查看次数: |
5747 次 |
| 最近记录: |