小编Ram*_*ngh的帖子

为什么我不能在firebase'then'回调中访问vuejs'this'?

我正在尝试使用Firebase向我的vuejs应用程序添加身份验证。注销用户后,应将用户发送回登录页面。我在HelloWorld.vue文件的脚本中实现了以下代码:

import firebase from 'firebase'

  export default {
    name: 'HelloWorld',
    data () {
      return {
        msg: 'Welcome to Your Vue.js App'
      }
    },
    methods: {
      logout () {
        firebase.auth().signOut().then(function () {
          this.$router.push('login')
        })
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

但是我从vuejs开发工具中收到以下错误:

TypeError: this is undefined
[Learn More]
app.js%20line%201954%20%3E%20eval:40:9
logout/<
HelloWorld.vue:37
Pb/e.a</e.g<
auth.js:23
Yb
auth.js:26
Ub
auth.js:26
h.Qb
auth.js:25
Cb
auth.js:19
Run Code Online (Sandbox Code Playgroud)

在Firebase回调中引用this对象

这个。$ router.push('login')

我需要帮助以弄清楚为什么我不能在回调中访问它以及如何解决该问题。提前致谢。

firebase firebase-authentication vue-router vuejs2

4
推荐指数
1
解决办法
828
查看次数