'Get the component data from the external JS function
Is it possible to get the component data from the external js function? I need to access markers, label and image.
Vue.component('home', {
template: '#home',
data: () => ({
markers: [],
label: '',
image: ''
}),
// Life-cycle methods
created() {
document.title = 'home';
ref.onSnapshot(snap => {
snap.docChanges().forEach(change => {
const { type, doc } = change;
if (type == 'added') {
addMarker(doc.id, doc.data()); // Call Method
}
});
});
}
});
// Function outside the Vue.component (within same file)
function addMarker(id, data) {
let m = new gm.Marker({
map,
animation: gm.Animation.DROP,
position: data.position,
label: data.label,
image: data.image // Custom
});
markers.push(m); // Access Component Data - markers
m.addListener('click', e => {
label = m.label; // Access Component Data - label
image = m.image; // Access Component Data - image
info.open(map, m);
});
}
Solution 1:[1]
You can pass the component instance to your function as an argument
// component
created() {
...
addMarker(this, doc.id, doc.data())
...
}
// external function
function addMarker(vm, id, data) {
...
vm.markers.push(m)
...
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Romalex |
