【问题标题】:Uncaught TypeError: Cannot read property '$bvModal' of undefined vue未捕获的类型错误:无法读取未定义 vue 的属性“$bvModal”
【发布时间】:2020-09-18 09:31:06
【问题描述】:

当我尝试调用时,我猜 this 在我的函数中的范围不正确 this.$bvModal.show("signinModal") 显示我的模态我收到此错误:

Uncaught TypeError: Cannot read property '$bvModal' of undefined

我知道$bvModal 应该限定为组件,并且我没有使用任何箭头函数,所以我看不到我的问题来自哪里,有没有办法在 vue 中解决这个问题?我失踪了?

<template>
  <div class="container">

    <b-modal id="signinModal" @hidden="signinUser($event)" :no-close-on-backdrop="true">
      <input id="email" v-model="email" placeholder="email">
      <input id="pass" v-model="password" placeholder="password">
    </b-modal>

    <form v-on:submit.prevent class="cube" v-if="modalShow == false">
       <div>
         <input id="title" v-model="title" placeholder="title">
       </div>

       <div>
         <textarea id="body" v-model="body" placeholder="body"></textarea>
       </div>

       <div>
         <b-button id ="postbtn" @click="addpost($event)" variant="outline-success">Publish</b-button>
       </div>    
    </form>
  </div>
</template>

<script>
const {fb,db} = require('../../fireconfig.js')

export default {
  name: 'AddPost',
  data:function(){   
    return{
        title: '',
        body: '',
        modalShow: true,
        email: '',
        password: ''
    }      
  },
  mounted(){
    this.$bvModal.show("signinModal")
  },

  methods:{
    signinUser:function(e){
      e.preventDefault()
      fb.auth()
        .signInWithEmailAndPassword(this.email, this.password)
        .catch(function(error) {
          // Handle Errors here.
          var errorCode = error.code;
          var errorMessage = error.message;

          if (errorCode === 'auth/wrong-password' || errorCode === 'auth/invalid-email') {
            alert('password or email incorrect',errorCode,errorMessage);
            this.$bvModal.show("signinModal") //---------------ERROR THROWN HERE

         } else {
            console.log('user successfully authenticated')
         }
      });
    },

    addpost(event){   
      event.preventDefault()
      try {
        let data = {
           title: this.title,
           body: this.body
        }

        db.collection('articles').doc(this.title).set(data)
        console.log('uploaded data to db')
      } catch(err) {
        console.log('there was an error',err)
      }
    },
  } 
}
</script>

【问题讨论】:

    标签: javascript vue.js bootstrap-modal this


    【解决方案1】:

    在你的catch 中将你的函数变成一个箭头函数,它会在vue 组件中引用this

    signinUser:function(e){
        e.preventDefault()
       fb.auth().signInWithEmailAndPassword(this.email, this.password)
              .catch( error => {
                  // Handle Errors here.
                  var errorCode = error.code;
                  var errorMessage = error.message;
    
            if (errorCode === 'auth/wrong-password' || errorCode === 'auth/invalid-email') {
                  alert('password or email incorrect',errorCode,errorMessage);
                  this.$bvModal.show("signinModal") //---------------ERROR THROWN HERE
    
                } else {
                  console.log('user successfully authenticated')
    
                }
        });
      },
    

    【讨论】:

    • 谢谢!效果很好,我能问一下为什么会这样吗?我认为箭头函数内部的任何东西都被限定在它们自己的范围内?
    • @monsterpiece 箭头函数不绑定自己的范围。它将从父级使用,在本例中为 Vue 组件。
    猜你喜欢
    • 2021-05-03
    • 2021-04-03
    • 2021-12-22
    • 2015-01-06
    • 2017-07-26
    • 2019-02-26
    • 2021-12-25
    相关资源
    最近更新 更多