vue方法內的方法使用this的問題

locapos(){//定位方法
if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
              var pos = {
                lat: position.coords.latitude,
                lng: position.coords.longitude
              };
            var latLng = new google.maps.LatLng(aa[1],
               aa[0]);
              var infoWindow = new google.maps.InfoWindow({
                        content: "當前位置:<br/>經度:" + pos.lat + "<br/>維度:" + pos.lng
                        //提示窗體內的提示信息
                    });
              infoWindow.setPosition(latLng);

               _this.mark = new google.maps.Marker({
                        position: latLng,    //將前面設定的座標標註出來
                        icon:image,
                        animation: google.maps.Animation.BOUNCE,//添加動畫效果
                        map: map //將該標註設置在剛纔創建的map中
                    });
        //             //標註提示窗口

        //             //打開提示窗口
                    infoWindow.open(map, mark);

            }, function() {
              handleLocationError(true, infoWindow, map.getCenter());
            });
          } else {
            // Browser doesn't support Geolocation
            handleLocationError(false, infoWindow, map.getCenter());
          }

methods中定義一個locapos()定位方法,其中想在內部方法中使用this調用data()中定義的marker一致不成功,提示未定義,經過不懈努力終於找到解決方法,增加一句話就OK了!

var _this = this;

如此簡單,豁然開朗!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章