寫代碼中犯的錯誤(1)

vue使用axios傳遞數據給後端

data() {
    return {
      provinceOptions: [],
      marker:{
        lat:"緯度",
        lng:"經度",
        province:"省份",
      },
    }
}

在data中命名了這兩個數據,一個是list,一個是json對象;當我嘗試將marker添加到provinceOptions中時出現了問題。
我使用瞭如下的命令,

provinceOptions.push(marker)

// 添加數據後:provinceOptions中存儲瞭如下數據
[{lat:"", lng:"", pvovince:""}]

第一次存入了空值,之後幾次都不會出現這種情況,而且如果不存入json數據,而是直接存字符串數據也不會出現這個問題。在這裏我卡了非常久,因爲其實我的應用場景是axios向後臺請求數據之後,將數據存入provinceOptions中,所以我還以爲是axios提交post出現了問題(因爲會出現跨域,options驗證問題)。

一直以爲是json/axios的問題,後來發現原來是百度地圖接口的問題(口吐芬芳)

反正要提醒的一個點就是,一定要在 if (result) { }中調用代碼!!sorry,這篇當做給我自己一個提醒,要用到百度地圖接口的也可以注意。

this.myGeo = new BMap.Geocoder()
      this.myGeo.getLocation(point, function (result) {
        if (result) {
          _this.newmarker.city= result.addressComponents.city
          _this.newmarker.province = result.addressComponents.province
          _this.newmarker.area = result.addressComponents.district
          _this.newmarker.adcode = _this.matchadcode(_this.newmarker.area)
          if(result.addressComponents.street==""){
            _this.newmarker.street = result.business.split(",")[0]
          }else{
            _this.newmarker.street = result.addressComponents.street
          }
          _this.newmarker.lat = result.point.lat
          _this.newmarker.lng = result.point.lng
          // console.log(_this.newmarker)
          _this.postProPositionMap(_this.newmarker)
        }
      })
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章