【问题标题】:i can't access data values from methods in my vue.js component?我无法从我的 vue.js 组件中的方法访问数据值?
【发布时间】:2017-12-03 10:25:01
【问题描述】:

我无法在maps() 方法中从data() 访问值lat , lng

我的 vue.js 组件 代码链接:https://gist.github.com/melvin2016/c8082e27b9c50964dcc742ecff853080

lat,lng 的控制台图像 enter image description here

<script>
import Vue from 'vue';
import navbarSec from './navbarSec.vue';
export default {
  data(){
    return{
      lat: '',
      lng: '',
      mapState: window.mapState,
      from:'',
      to:'',
      placesFrom:[],
      placesTo:[]
    };
  },
  components:{
    'navbar':navbarSec
  },
  created(){
    var token = this.$auth.getToken();
    this.$http.post('http://localhost:3000/book',{},{headers: {'auth':token}}).then(function(data){
      this.session = true;
    })
    .catch(function(data){
      this.session = false;
      this.$auth.destroyToken();
      Materialize.toast(data.body.message, 6000,'rounded');
      this.$router.push('/login');
    });
    if(navigator.geolocation){
      navigator.geolocation.getCurrentPosition((data)=>{
        this.lat = data.coords.latitude;
        this.lng = data.coords.longitude;
        this.from=data.coords.latitude+' , '+data.coords.longitude;
      });
    }else{
      Materialize.toast("Cannot Get Your Current Location !", 6000,'rounded');
    }
  },
  mounted(){
    if (this.mapState.initMap) {// map is already ready
      var val = this.mapState.initMap;
      console.log(val);
      this.maps();
    }
  },
  watch: {
    // we watch the state for changes in case the map was not ready when this
    // component is first rendered
    // the watch will trigger when `initMap` will turn from `false` to `true`
    'mapState.initMap'(value){
      if(value){
        this.maps();
      }
    },
    from : function(val){
      if(val){
        var autoComplete = new google.maps.places.AutocompleteService();
        autoComplete.getPlacePredictions({input:this.from},data=>{
          this.placesFrom=data;
        });
      }
    },
    to:function(val){
      if(val){
        var autoComplete = new google.maps.places.AutocompleteService();
        autoComplete.getPlacePredictions({input:this.to},data=>{
          this.placesTo=data;
        });
      }
    }
  },
  methods:{
    maps(){
      var vm = this;
      var lati = vm.lat;
      var lngi = vm.lng;
      console.log(lati+' '+lngi);
      var map;
      var latlng = {lat: lati, lng:lngi };
      console.log(latlng);
        this.$nextTick(function(){
          console.log('tickkkk');
           map = new google.maps.Map(document.getElementById('maplo'), {
            zoom: 15,
            center: latlng
          });
          var marker = new google.maps.Marker({
            position: latlng,
            map: map
          });
        });
    }
  }
}
</script>

【问题讨论】:

  • 您正在访问它们,它们只是没有数据。你期望发生什么?
  • 我看到您是根据this.lat = data.coords.latitude; 分配的。您确定这部分数据分配正确吗?
  • 是的,它正在正确分配值。我不知道为什么会这样?

标签: javascript google-maps-api-3 vue.js frontend


【解决方案1】:

发生这种情况是因为您在挂载时调用maps(),此时navigator.geolocation.getCurrentPosition((data) =&gt; {}) 代码尚未解析。考虑到这一点,在getCurrentPosition 方法中调用this.maps(),即:

navigator.geolocation.getCurrentPosition((data)=>{
    this.lat = data.coords.latitude;
    this.lng = data.coords.longitude;
    this.from=data.coords.latitude+' , '+data.coords.longitude;
    this.maps()
});

我没有详细查看,但您可以更改 maps() 方法中的位以在执行此操作时删除 nextTick 内容,因为您将在循环的稍后时间调用它哪一点都将被渲染。

【讨论】:

  • @MelvinGeorge 太好了,很高兴听到。不要忘记勾选它作为答案。
猜你喜欢
  • 2020-10-30
  • 1970-01-01
  • 2021-02-13
  • 2018-02-23
  • 2018-01-02
  • 1970-01-01
  • 2018-04-27
  • 1970-01-01
  • 2021-10-28
相关资源
最近更新 更多