javascript - VueJS $emit 无法将数据推送到父级

标签 javascript vuejs2 emit

因此,我在让 $emit 向父级发送数据时遇到问题。我想要做的是单击提交按钮从输入中获取数据,然后将结果发送给家长。我使用原型(prototype) JSON block 只是为了测试流程,因为我是 Vue 新手。

这里有一些填充文本,因为我需要克服代码过多与文本不足的难题。这里有一些填充文本,因为我需要克服代码过多与文本不足的难题。

TypeError: Cannot read property '$emit' of undefined at eval (SearchBar.vue?e266:42)

<template>
    <div class="searchbox">
         <datepicker v-model="fromdate" placeholder="Start Date" name="fromdate" class="datepicker"></datepicker>
         <datepicker v-model="todate" placeholder="End Date" name="todate" class="datepicker"></datepicker>
         <input type="text" v-model="namesearch" name="namesearch" placeholder="Username/Lastname"/>
         <input type="text" v-model="titlesearch" name="titlesearch" placeholder="Title / Short Desc."/>
         <input type="text" v-model="descsearch" name="descsearch" placeholder="Long Desc"/> 
         <button name="refreshresults" v-on:click="getresults">Do Search</button>
    </div>
</template>

<script>
import Datepicker from 'vuejs-datepicker/dist/vuejs-datepicker.common.js'
import axios from 'axios'
export default {
    name: 'SearchBar',
    components:{
     Datepicker
  },
  data(){
    return {
    fromdate:"",
    todate:"",
    namesearch:"",
    titlesearch:"",
    descsearch:""
    }
  },
  methods:{
      getresults: function(e){
        const searchcriteria = {
            fromdate: this.fromdate,
            todate: this.todate,
            namesearch: this.namesearch,
            titlesearch: this.titlesearch,
            descsearch: this.descsearch
        }
            axios.get('https://jsonplaceholder.typicode.com/todos')
            .then(function (response) {
                // handle success
                console.log(response.data);
                this.$emit('sendingInfo',searchcriteria);
            })
            .catch(function (error) {
                // handle error
                console.log(error);
            })
            .then(function () {
                console.log('catchall');
            });

    }
  }
}
</script>

<style>
.vdp-datepicker{
    display:inline-block;
}

INPUT,BUTTON{
    margin:5px;
    display: inline-block;
    padding:5px;
    text-align: center;
    font-weight: bold;
    font-size: 15px;
}
</style>

最佳答案

这是因为 axios .then(function() {...}) 回调中的 this 并未引用您的 VueJS 组件。如果您尝试将 this 记录到控制台,您将看到它不再指向您的 VueJS 组件。

既然你用的是ES6,就用箭头函数吧,这样就preserves the lexical this from the enclosing scope :

axios.get('https://jsonplaceholder.typicode.com/todos')
  .then(response => {
      // handle success
      console.log(response.data);
      this.$emit('sendingInfo',searchcriteria);
  })

关于javascript - VueJS $emit 无法将数据推送到父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54972782/

相关文章:

C# Emit ,如何编写 if 语句

javascript - 在 JavaScript 中确定域名?

javascript - Angular ui-router解决服务暂停路由更改

javascript - 从组件访问 Vuex Store 中的 Mutator 会产生未知突变类型错误

c# - Reflection.Emit - IL - 对象上的调用方法

map - 为什么发出(meta.id,NULL)

javascript - 使用 form.action 时,我的 aspx 页面中的更新面板无法工作

javascript - 如何使用 jQuery 获取自定义标签的值

javascript - 获取锥形梯度 polyfill 以与 Vue 一起工作

javascript - 如何定期更新 VueJS 中的组件