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>
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.
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.
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.
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 .
I advise you to use vue2-leaflet instead.
It has a popup component that behaves the way you describe. See 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 !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With