0

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);
    });
}

1 Answer 1

0

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)
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.