Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.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
Vue.js 在vue js中获取地理坐标_Vue.js_Geolocation_Lex_Vuex - Fatal编程技术网

Vue.js 在vue js中获取地理坐标

Vue.js 在vue js中获取地理坐标,vue.js,geolocation,lex,vuex,Vue.js,Geolocation,Lex,Vuex,我要客户端当前的地理位置,为了准确,我把10000超时 var options = { timeout: 10000 }; 这是js函数 function getCoordinates() { return new Promise(function(resolve, reject) { if ( !("geolocation" in navigator) || !("getCurrentPosition" in navigator.ge

我要客户端当前的地理位置,为了准确,我把10000超时

 var options = {
      timeout: 10000
    };
这是js函数

function getCoordinates() {
  return new Promise(function(resolve, reject) {
    if (
      !("geolocation" in navigator) ||
      !("getCurrentPosition" in navigator.geolocation)
    ) {
      return Promise.reject(new Error("geolocation API not available"));
    }
    var options = {
      timeout: 10000
    };

    // browser prompts for permission
    navigator.geolocation.getCurrentPosition(
      getPositionCallBack,
      reject,
      options
    );

    function getPositionCallBack(position) {
      var coords = "";
      try {
        coords = {
          lat: position.coords.latitude,
          long: position.coords.longitude
        };
      } catch (err) {
        return reject(err);
      }
      return resolve(coords);
    }
  });
}
商店:

var Ui = new WebUi({
coordincates:'',//initialise
});

export default {
  name: "app",
  store: Ui.store,
  components: { Page },
  data() {
    return {
      favIcon: getFavIcon()
    };
  }
};
承诺完成后更新WebUi坐标

我在获取坐标后再次更新存储,然后在vue js中获取以下错误

Do not mutate vuex store state outside mutation handlers.
我修改了store/index.js 更新


现在的问题是如何从javascript更新我的存储坐标。

如果将
scrict
设置为
false
,您将能够从以下任何组件更改存储的状态:

//在组件中使用此代码(仅当strict:false时不会抛出警告)
这个.$store.state.coordinates={x:1,y:2};
注意名称是
坐标
,在您的示例中,您的商店是
坐标
,我假设这是一个打字错误

如果商店中不存在坐标,请使用:

//在组件中使用此代码(仅当strict:false时不会抛出警告)
//如果存储区中可能还不存在该属性,请使用Vue.set()
set(this.$store.state,'coordinates',{x:1,y:2});

也就是说,最好是将
严格
改为
真实
并创建。由于您使用的是
WebUi
,因此可能有特定的方法。

显示您的店铺声明。显示您试图更新它的位置。HI@acdcjunior I updated question,在
var Ui=new WebUi中什么是
WebUi
({
?这是一个类似vuexCan的库,您可以指向它的链接吗?谢谢您的回答,但是,我如何从javascript更新存储?我确实导入了存储,这个。$store给未定义的
这个。$store.state.coordinates={x:1,y:2};
部分。我使用了store.state.sessionAttributes.coordinates=JSON.stringify(coords);console.log给出了console.log(store.state.sessionAttributes.coordinates)的实际值;但实际上它并没有更新实际的存储
strict: false,