淺談vue方法內的方法使用this的問題
更新時間:2018年09月15日 08:57:47 作者:白菜和蘿卜
今天小編就為大家分享一篇淺談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 //將該標注設置在剛才創(chuàng)建的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;
如此簡單,豁然開朗!
以上這篇淺談vue方法內的方法使用this的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue中el-autocomplete與el-select的異同
本文主要介紹了vue中el-autocomplete與el-select的異同,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05