我正在使用Open Street Map进行我的NUXT.js
项目,并且想通过store
方法访问mounted()
。我收到错误消息,指出未在mounted()
方法名称空间中定义存储。我该如何访问?
@ / assets / js / utils.js
const utils = {
//...
updateMap(context) {
let _context = context;
const checkMapObject = setInterval(() => {
if (_context.$refs.map) {
_context.map = _context.$refs.map.mapObject;
clearInterval(checkMapObject);
//console.log(_context.map);
if (process.browser) {
L = require('leaflet');
console.log("MAP OBJECT INITED");
delete L.Icon.Default.prototype._getIconUrl;
L.Icon.Default.mergeOptions({
iconRetinaUrl: require('leaflet/dist/images/marker-icon-2x.png'),
iconUrl: require('leaflet/dist/images/marker-icon.png'),
shadowUrl: require('leaflet/dist/images/marker-shadow.png'),
});
for (let marker of this.getMapMarkers(_context.store.state.pollingStations)) {
L.marker(marker).addTo(_context.map)
}
}
}
}, 100);
},
// ...
}
export default utils;
index.vue
<template>
...
</template>
<script>
...
import utils from '@/assets/js/utils'
export default {
// ...
mounted() {
utils.updateMap(this)
},
// ...
}
</script>
我已经收到此错误:
Uncaught TypeError: Cannot read property 'state' of undefined