Vue.js 在vue js中获取地理坐标
我要客户端当前的地理位置,为了准确,我把10000超时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
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,