关于vue中父子组件传值报错的问题解决过程记录:this.$emit is not a function

报错基本上都是因为this指向问题,基本上就是 function 和 () => 之间的问题,进行这俩的替换基本上都可以解决。我碰到了此问题,因为this指向了方法,而不是vue,报错代码:

mounted () {
    lazyAMapApiLoaderInstance.load().then(() => {
      // your code ...
      this.map = new AMap.Map("amapContainer", {
        center: [121.59996, 31.197646],
        zoom: this.zoom
      });
 
      this.map.on('click', function (e) {
        const data = getLngLat(e)
        this.$emit("lnglat", data)
      });
    });
  },

有两种解决方式:

第一种方式,对象中转:


mounted () {
    const _this = this
    lazyAMapApiLoaderInstance.load().then(() => {
      // your code ...
      this.map = new AMap.Map("amapContainer", {
        center: [121.59996, 31.197646],
        zoom: this.zoom
      });
 
      this.map.on('click', function (e) {
        const data = getLngLat(e)
        _this.$emit("lnglat", data)
      });
    });

第二种方式,使用箭头函数:


mounted () {
    lazyAMapApiLoaderInstance.load().then(() => {
      // your code ...
      this.map = new AMap.Map("amapContainer", {
        center: [121.59996, 31.197646],
        zoom: this.zoom
      });
 
      this.map.on('click', (e) => {
        const data = getLngLat(e)
        this.$emit("lnglat", data)
      });
    });
  }

 

以上方案参考大佬博文  地址:https://blog.csdn.net/yssa1125001/article/details/107449755

我的问题是自己在父页面写错了:

使用@ 而不是 :

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值