Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps 在vue组件内的javascript实例化组件上添加事件处理程序。(google.maps.DirectionsRenderer)_Google Maps_Vue.js - Fatal编程技术网

Google maps 在vue组件内的javascript实例化组件上添加事件处理程序。(google.maps.DirectionsRenderer)

Google maps 在vue组件内的javascript实例化组件上添加事件处理程序。(google.maps.DirectionsRenderer),google-maps,vue.js,Google Maps,Vue.js,我们有一个vue应用程序,其中包含一个使用google directions API的google应用程序组件。 为此,我们启动映射,并创建DirectionsService实例和DirectionsRenderer实例。我们需要在路由更改时调用一个函数 methods: { initMap() { if (this.$refs.baseMap) { this.$refs.baseMap.$mapPromise.then(map => {

我们有一个vue应用程序,其中包含一个使用google directions API的google应用程序组件。 为此,我们启动映射,并创建DirectionsService实例和DirectionsRenderer实例。我们需要在路由更改时调用一个函数

methods: {
    initMap() {
      if (this.$refs.baseMap) {
        this.$refs.baseMap.$mapPromise.then(map => {

          this.directionsService = new google.maps.DirectionsService

          this.directionsRenderer = new google.maps.DirectionsRenderer({
              draggable: true,
              map: map,
            })

          this.directionsRenderer.addListener('directions_changed', 
            function() {
              this.computeTotalDistance()  // I need this to be a called when the direction change
            }
          )
        })
      }
    },
    computeTotalDistance(){
      // do some code
    }

上面的代码不理解什么是“this”,因为它位于addListener函数中。如何使其工作?

将变量引用添加到
应能工作:

const self = this
this.directionsRenderer.addListener('directions_changed', 
     function() {
         self.computeTotalDistance()  // I need this to be a called when the direction change
      }
)
副本