这是我的网页
Set sht = ActiveSheet
For Each co In sht.ChartObjects
co.Activate
For Each sc In ActiveChart.SeriesCollection
temp = sc.Formula
'Counting commas in formula
counter = Len(temp) - Len(Replace(temp, ",", ""))
'If commas were 3 then 4 params
If counter = 3 Then
sc.Formula = "=SERIES(,,1,1)"
'If commas were 4 then 5 params
ElseIf Count = 4 Then
sc.Formula = "=SERIES(,,,1,1)"
End If
sc.Formula = temp
Next sc
Next co
假设我已经登录了用户。当我在控制台中加载页面时,我得到<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Calcolo Diliuzioni</title>
</head>
<body>
<h1>My app</h1>
<!-- Libreria per gestire l'autenticazione con google -->
<script src="https://apis.google.com/js/platform.js" async defer></script>
<!-- Firebae config -->
<script src="https://www.gstatic.com/firebasejs/live/3.0/firebase.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "",
authDomain: "",
databaseURL: "",
storageBucket: "",
};
firebase.initializeApp(config);
</script>
<script type="text/javascript">
var user = firebase.auth().currentUser;
if (user) {
console.log(user);
} else {
console.log(user);
}
</script>
</body>
</html>
。虽然我期望拥有当前用户的对象。
如果我在浏览器控制台中运行相同的代码
null
我能够获取当前用户的对象。
我认为var user = firebase.auth().currentUser;
if (user) {
console.log(user);
} else {
console.log(user);
}
有一些异步行为。解决问题的正确方法是什么?我应该在firebase.initializeApp(config);
函数上使用承诺吗?一种回调..
答案 0 :(得分:14)
初始化应用是同步的。但是,确定当前用户状态是异步的。确定当前身份验证状态的正确方法是使用观察者: https://firebase.google.com/docs/auth/web/manage-users
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in and currentUser will no longer return null.
} else {
// No user is signed in.
}
});
答案 1 :(得分:8)
我将两种方法结合在一起:
function canActivate(){
return new Promise(resolve => {
if (firebase.auth().currentUser) {
resolve(true);
} else {
const unsubscribe = firebase.auth().onAuthStateChanged(function (user) {
unsubscribe();
if (user) {
resolve(true);
} else {
resolve(false);
}
});
}
});
}
在SPA,如果用户已登录且用户可以转到任何页面,请firebase.auth().currentUser == true
。
但是如果用户重新加载页面,请获取firebase.auth().currentUser == false
。
用户需要等待firebase.initializeApp(config);
完成,所以添加
firebase.auth().onAuthStateChanged
等待init完成。