繁体   English   中英

为什么这个错误会出现 Uncaught (in promise) TypeError: Cannot read property 'created' of undefined in vue.js

[英]why is this error coming Uncaught (in promise) TypeError: Cannot read property 'created' of undefined in vue.js

  1. 这是我在 vue.js 中实现的代码
  2. 我试图向 firebase 数据库添加一个新员工。
  3. 代码一直在工作,直到我编写了方法并初始化了数据部分
  4. 当我尝试返回运行代码的步骤时,它仍然出现运行时错误
<template>
  

  <div class="container-fluid">
    <center><h1>New Employee</h1></center>
    <form @submit.prevent="adds" class="form">
      <label>Enter Name</label>
      <input type="text" class="form-control" v-modle="name">
      <label>Enter Employee ID</label>
      <input type="text" class="form-control" v-modle="emp_id">
      <label>Enter Department</label>
      <input type="text" class="form-control" v-modle="dep">
      <label>Enter Position</label>
      <input type="text" class="form-control" v-modle="position" >
      <router-link to="/" class="btn btn-danger btn-lg">Cancel</router-link>
      <button type="submit" class="btn btn-outline-success btn-lg">Submit</button>
    </form>

  </div>
  
</template>

<script>
import db from '../components/firebaseinit.js'
export default {
  name:"newEmployee",
  data(){
    return{
       name: null,
       emp_id: null,
       dep: null,
       position: null
    }
  },
  methods:{
    adds(){
        db.collection('employee').add({
          emp_id: parseInt(this.emp_id),
          name: this.name,
          dep: this.dep,
          position: this.position
        }).then(this.router.push("/")).catch(err => console.log(err))
    
    }
  }
}
</script>
  1. 我在第 42 行发现了我的代码的问题
then(this.router.push("/")).catch(err => console.log(err))
  1. 调用路由器的正确方法是使用 $ 代码将替换为
then(() => this.$router.push("/")).catch(err => console.log(err))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM