【问题标题】:where to place firebase.auth().onAuthStateChanged() in Vuejs在 Vuejs 中放置 firebase.auth().onAuthStateChanged() 的位置
【发布时间】:2018-05-12 03:44:27
【问题描述】:

我需要检查用户是否使用 Firebase 登录。为此,我们使用

        firebase.auth().onAuthStateChanged(function(user){
       // example
       this.user = user
       })

我们也在使用 Vue.js,所以我想知道它是否应该实现为观察者、方法甚至计算属性。当然还有如何实施。

【问题讨论】:

标签: firebase vue.js firebase-authentication


【解决方案1】:

main.js文件的created ()方法中

【讨论】:

  • 谢谢,但是在Node环境下,应该放在Vue根实例的created ()还是组件中?
【解决方案2】:

如果我不使用 Vuex,我会以这种方式使用 FBase:

<script>
import * as firebase from 'firebase'

return default {
  el: '#app',
  data: {
    user: '',
    fb: null,
    fbConf: {
      apiKey: 'your API key',
      authDomain: '<your-domain>.firebaseapp.com'
    }
  },
  methods: {
    logIn (user) {
      this.user = user.uid
    },
  },
  created () {
    this.fb = firebase
    this.fb.initializeApp(this.fbConf)
    this.fb.auth().signInAnonymously()
    this.fb.auth().onAuthStateChanged(this.logIn)
  }
}
</script>

【讨论】:

  • 如果你使用 Vuex 会怎样?
猜你喜欢
  • 2018-01-02
  • 2020-08-06
  • 2018-01-03
  • 2016-10-02
  • 2021-06-03
  • 1970-01-01
  • 2019-03-26
  • 2018-08-17
  • 1970-01-01
相关资源
最近更新 更多