无法读取未定义的属性“ $ router”?

时间:2019-03-22 22:10:20

标签: javascript vue.js

我正在尝试使用户可以登录到页面,但是正在向我显示此消息“无法读取未定义的属性'$ router'”

我在Vue中登录的代码,该代码位于“导出默认值”内部

import {fb} from "../firebase";
import router from '../router';

export default {
 name: "NavBar",
 components: {...},
 data() {
   return {
    login: false,
    register: false,
    name: null,
    email: null,
    pwd: null,
  };
 },
methods: {
logInner(){
      fb.auth().signInWithEmailAndPassword(this.email, this.pwd)
          .then(function(){
          this.$router.replace('users');
          })
          .catch(function(error) {`enter code here`
              // Handle Errors here.
              var errorCode = error.code;
              var errorMessage = error.message;
              if (errorCode === 'auth/wrong-password') {
                  alert('Wrong password.');
              } else {
                  alert(errorMessage);
              }
              console.log(error);
      });

  },
registering() {
  fb.auth().createUserWithEmailAndPassword(this.email, this.pwd)
    .then((user) =>{
      this.$router.replace('users');
    })
    .catch(function(error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      if (errorCode == "auth/weak-password") {
        alert("The password is too weak.");
      } else {
        alert(errorMessage);
      }
      console.log(error);
     });
   }
 }
 };

我已经尝试过router.push甚至router.go都没有帮助。 有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

this在fb.auth()函数中不可用,请尝试以下操作。有关详细说明,请查看此答案:https://stackoverflow.com/a/47148828/9489397

logInner(){
let self = this
      fb.auth().signInWithEmailAndPassword(this.email, this.pwd)
          .then(function(){
          self.$router.replace('users');
          })

registering() {
let self = this
  fb.auth().createUserWithEmailAndPassword(this.email, this.pwd)
    .then((user) =>{
      self.$router.replace('users');
    })