Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I integrate a Vue component in a leaflet popup? [duplicate]

My project uses Vue.js and leaflet to display some data on a map, in the shape of markers.

The markers on the map are bound popups, which content I would like to be reactively updated when the associated data change, just like any Vue component does.

I checked the option of vueleaflet, which provides a popup component for leaflet. But this script defines the content of the popup though an attribute of the component: <l-popup content="a popup"></l-popup>. This way isn't so relevant in my case, as I have some complex templating to insert in the popup (including conditional statements and subcomponents).

I would rather need something like:

<l-popup>
  My elaborate content here.
</l-popup>
like image 951
Abrab Avatar asked Jun 21 '19 04:06

Abrab


People also ask

How do I share data between Vue components?

There are only three different ways to share data across the VueJs components, it depends on you which technique is better for you to solve your problem. Using props share data from parent to child. Using Event Emitting custom events to share data from child to parent.

How do I create a pop up in leaflet?

Use the addPopups() function to add standalone popup to the map. A common use for popups is to have them appear when markers or shapes are clicked. Marker and shape functions in the Leaflet package take a popup argument, where you can pass in HTML to easily attach a simple popup.

How do I recreate a component Vue?

The best way to force Vue to re-render a component is to set a :key on the component. When you need the component to be re-rendered, you just change the value of the key and Vue will re-render the component.

Does Vue reuse components?

Since components are reusable Vue instances, they accept the same options as new Vue , such as data , computed , watch , methods , and lifecycle hooks. The only exceptions are a few root-specific options like el .


1 Answers

I advise you to use vue2-leaflet instead.

It has a popup component that behaves the way you describe. See here.

enter image description here

There is also another solution, if you don't want to use a package to do the link between vue and leaflet. It's almost a trick.

You create your elaborate content in a component, but you hide it :

<my-elaborate-popup-content v-show=False ref='foo'><my-elaborate-popup-content>

Then you can access the generated html of that component in your code like this :

const template = this.$refs.foo.$el.innerHTML

and use it to fill your popup !

like image 83
Istopopoki Avatar answered Sep 30 '22 07:09

Istopopoki