【问题标题】:Vuejs, Get content of a $event.emitVuejs,获取 $event.emit 的内容
【发布时间】:2020-02-02 00:35:22
【问题描述】:

我正在尝试获取 $event.emit 的内容,如下所示。在第一个 console.log 中,函数内部包含内容。离开函数,而不是变量的console.log。

mounted () {
  this.$events.on('emitEvent', function (eventData) {
    this.line = _.cloneDeep(eventData)
    console.log('1', this.line)
  })
  console.log('2', this.line)
}

我正在使用this package 进行事件处理。

【问题讨论】:

  • 这是什么console.log('1, this.line)
  • 在第一个console.log中,显示了对象的内容。在第二个 console.log 中,功能失效,没有返回给我
  • 你已经放了一个单引号。是故意的吗?
  • 这里this的上下文与vue组件不同。因此,要访问您的数据属性,您可以使用闭包而不是函数
  • 你在使用this package吗?据我所知,默认的 Vue 实例中没有 $events 对象...

标签: javascript vue.js vue-events


【解决方案1】:

尝试这样做:

mounted() {
   var $that = this;

   this.$events.on('emitEvent', function (eventData) {
        $that.line = _.cloneDeep(eventData);
        console.log('1', $that.line);
   })

   console.log('2', this.line);
}

【讨论】:

    【解决方案2】:

    这应该可行:

    this.$events.on('emitEvent', (eventData) => {
        this.line = _.cloneDeep(eventData)
        console.log('1', this.line)
      })
      console.log('2', this.line)
     }
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2019-02-09
      • 2018-07-21
      • 2018-03-25
      • 2014-11-12
      • 2014-01-19
      • 2017-03-02
      相关资源
      最近更新 更多