Skip to content

How to reference FeatureGroup? #369

Closed
@airmalik

Description

@airmalik

I'd like to wrap markers within a featureGroup and then reference that feature group from the script. I'm using this.$refs to get a reference to the featureGroup inside the mounted hook. But the reference is null unless I wait at least a millisecond before accessing it.

  mounted: function () {
    // const fg = this.$refs.features.mapObject
    // console.log(fg.getBounds())
    // The above gives null error
    
    // This works
    setTimeout(this.bounds, 1)
  },
  methods: {
  	bounds: function () {
    	console.log(this.$refs.features.mapObject.getBounds())
    }
  }

I'm assuming this is because the slot in the LFeatureGroup.vue template isn't created until ready=true:

<template>
  <div style="display: none;">
    <slot v-if="ready" />
  </div>
</template>

...

  mounted () {
    this.mapObject = featureGroup();
    propsBinder(this, this.mapObject, this.$options.props);
    DomEvent.on(this.mapObject, this.$listeners);
    this.ready = true;
    this.parentContainer = findRealParent(this.$parent, true);
    if (this.visible) {
      this.parentContainer.addLayer(this);
    }
  }

So what's the correct way of getting a reference to the featureGroup without the timeout hack?

Live Demo

Here's the fiddle showing the issue: https://jsfiddle.net/fghrqe2n/1/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions