在Next.js项目中尝试初始化Firebase分析时,我收到错误ReferenceError: window is not defined
。
以下是相关的代码片段:
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
let analytics;
if (window !== undefined) {
analytics = getAnalytics(app);
}
onAuthStateChanged(auth, (user) => {
if (user) {
// User signed in
const uid = user.uid;
console.log(uid);
} else {
// User signed out.
}
});
export { app, auth, analytics };
这个问题在另一个职位上得到了解决;但提议的解决方案似乎对我不起作用。
如果window
不存在,则无法检查其类型。
考虑:
if (typeof window !== 'undefined') {