Javascript 在Nuxt JS中创建Vue应用之前检查Firebase onAuthStateChanged
我已经使用Vue JS v2有一段时间了,我对它很满意。最近,我开始使用Nuxt JS,到目前为止,我能够掌握大部分内容。但在花了无数个小时搜索web之后,我仍然无法在Nuxt JS中创建Vue应用程序之前找到如何使用firebaseJavascript 在Nuxt JS中创建Vue应用之前检查Firebase onAuthStateChanged,javascript,firebase,firebase-authentication,nuxt.js,Javascript,Firebase,Firebase Authentication,Nuxt.js,我已经使用Vue JS v2有一段时间了,我对它很满意。最近,我开始使用Nuxt JS,到目前为止,我能够掌握大部分内容。但在花了无数个小时搜索web之后,我仍然无法在Nuxt JS中创建Vue应用程序之前找到如何使用firebaseonAuthStateChanged() 让我解释一下 在VUE2中,我通常在main.js文件中执行操作 从“Vue”导入Vue 从“./App.vue”导入应用程序 从'@/firebase/init'导入{auth} 让app=null // ⚡ 在创建Vue
onAuthStateChanged()
让我解释一下
在VUE2中,我通常在main.js
文件中执行操作
从“Vue”导入Vue
从“./App.vue”导入应用程序
从'@/firebase/init'导入{auth}
让app=null
// ⚡ 在创建Vue应用程序之前,等待firebase auth初始化
身份验证onAuthStateChanged(()=>{
//初始化应用程序(如果尚未创建)
如果(!应用程序){
app=新Vue({
路由器,
商店,
渲染:h=>h(应用程序)
}).$mount(“#应用程序”)
}
})
但是在Nuxt JS中,整个main.JS文件被封装。现在我知道我可以使用plugin来做一些事情,比如Vue.use()
,但是这些事情还是在创建Vue应用程序之后发生的
我想将vue应用程序创建包装在firebase身份验证检查中,因此我认为没有办法实现这一点。所以,如果有人对如何实现这一点有任何想法,请让我知道
请注意我没有尝试根据身份验证状态进行任何重定向。我知道这里已经有了这些答案,我已经检查过了。我想我也遇到了类似的问题,我设法创建了一个登录流,但这感觉像是一个解决办法 登录时,我存储cookie和用户数据:
我也遇到了同样的问题,找到了一个更简单的解决办法。但是,我不知道为什么firebase nuxt中没有记录这一点 在
布局/default.vue
布局中,我实现了以下功能:
<template>
<div>
<div v-show="!isLoaded" class="loading">
</div>
<div class="app" id="app" v-if="loaded">
<NavBar :loggedIn="loggedIn" />
<Nuxt id="router-view" />
<Footer />
</div>
</div>
</template>
<script>
export default {
data() {
return {
loggedIn: false,
loaded: false
}
},
created() {
this.$fire.auth.onAuthStateChanged(() => {
this.loaded = true
const user = this.$fire.auth.currentUser;
if (user) {
this.loggedIn = true
}
})
},
...
}
</script>
导出默认值{
数据(){
返回{
洛格丁:错,
加载:false
}
},
创建(){
此.fire.auth.onAuthStateChanged(()=>{
this.loaded=true
const user=this.$fire.auth.currentUser;
如果(用户){
this.loggedIn=true
}
})
},
...
}
我认为这与在vueJS的main.js中使用它完全一样。通过使用v-if
仅在初始化firebase auth后执行主应用程序内容