Issues with Vue Router functionality in Leaflet Popup are causing unexpected behavior

Incorporating Leaflet and Vue together in my codebase using the vue2-leaflet wrapper has presented a challenge. Specifically, I am facing difficulties getting Vue $router to function within Leaflet's popup. Below is a snippet of my current code along with the attempts I have made:

<template>
  <l-map>
    <l-tile-layer :url="url" />
    <l-marker
      v-for="point in points"
      :key="point.id"
      :lat-lng="point.latLng"
      :icon="point.icon"
    >
      <l-popup :content="displayInfo(point)"/>
    </l-marker>
  </l-map>
</template>
<script>
...
    displayInfo(point) {
      // how it usually works: this.$router.push({ name: 'point', params: { id: point.id } })
     
      // Attempt 1
      // return '<div onclick="routeToPage(' + point.id + ')">' + point.id + '</div><br/>' + point.subject

      // Attempt 2
      // return '<div @click="routeToPage(' + point.id + ')">' + point.id + '</div><br/>' + point.subject

      // Attempt 3
      // return '<router-link to="{ name: \'point\', params: { id: ' + point.id + ' } }">' + point.id + '</router-link><br/>' + point.subject;

      return point.id + '<br/>' + point.subject;
    },

    routeToPage(id) {
      return this.$router.push({ name: 'point', params: { id }
    }

...
</script>

Attempt 1 The error displayed upon clicking the id within the popup is as follows.

(index):1 Uncaught ReferenceError: routeToReport is not defined
    at HTMLDivElement.onclick

Attempt 2 Clicking the id yields no response or behavior. It appears as normal text without any interaction. Upon inspection, only the following is shown

<div class="leaflet-popup-content" style="width: 301px;">
    <div @click="routeToPage">39105</div><br>
    Aliquid voluptas animi facilis ipsum ducimus doloremque consequatur nemo porro perferendis atque dolorum quo adipisci perferendis magnam
</div>

Attempt 3

<div class="leaflet-popup-content" style="width: 301px;">
    <router-link to="{ name: 'point', params: { id: 39105 } }">39105</router-link><br>
    Aliquid voluptas animi facilis ipsum ducimus doloremque consequatur nemo porro perferendis atque dolorum quo adipisci perferendis magnam
</div>

None of these methods seem to convert the text into a link or recognize it as a route. Any insights on where I might be going wrong?

Please let me know if further details are required from my end or if the issue needs more clarification.

Answer №1

It's a good idea to structure the content of the popup in this way:

<l-popup>
  <div @click= ....> </div>
</l-popup>

By doing this, the @click within the div will be recognized as a template by vue. Placing it in :content would simply be treated as plain text.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to efficiently search MongoDB for existing records and group them by unique array values

My dataset is structured like this: { "_id" : 1, "category" : [ { "name": "category1" }, { "name": "category2" } ], "event": [ { type: "house" ...

The function socket.on(..) is not being triggered

Currently, I am in the process of developing a straightforward website to showcase socket communication and chat capabilities. The server side is coded using Python Flask app, while the client-side utilizes JavaScript. Below is an excerpt from the server c ...

Node.js is known for its ability to export both reference and pointer values

Having an issue with exporting my routing table from my express application. In the /bin/www.js file, there is a global variable representing the routing table: var routingTable = []; To create a simple route, new routing objects are pushed using the se ...

Tips on reloading or refreshing a react-table component using my form component

I currently have two reactJS components set up: CustomerForm component, which includes a form along with form handling code. CustomerList component, which utilizes react-table to list the customers. Both components are fully functional and operational. ...

Implementing beforeSend and complete in all instances of ajaxForm throughout the entire application as a universal

Is there a way to use the beforeSend and complete functions on all ajaxForms within a project without having to repeatedly insert the same code throughout the entire project? I have managed to achieve this by adding the following code for each form indivi ...

Tips for efficiently sending multiple ajax requests

Seeking help with my ajax knowledge. Currently, I have a function that is supposed to insert multiple items into an SQL database using a PHP page. However, it seems to only be inserting the last item entered. Here's the code snippet: function submitI ...

Common JavaScript Framework Startup Errors

Currently, I am delving into the world of JavaScript and experimenting with various thingamajigs. Could someone kindly shed some light on why my script is throwing errors? // Effects object var effects = { // Display an object show : function(obj) { o ...

Modify data in an array using Vuex

When working with my Vuex mutation, I am trying to replace an element in an array within the state. The code snippet below illustrates what I am attempting to do: UPDATE_MAILING(state, mailing) { let index = _.findIndex(state.mailings, {id: mailing.id ...

Order comments based on their category using vue.js

I have a component form with select filter: <template> <div class="form-group"> <select name="" v-model="filterRev" @change="filterReviews(filterRev)" class="form-control" id=""> <option ...

Finding matches within a specific group in regular expressions

I am currently tackling the challenge of implementing a feature that involves detecting and linking phrases like "Co. Reg. No" in a specific HTML element. <div class="entry">The company with Co. Reg. No 1241515 will...</div> My goal is to cre ...

Select the text inside the current cell of a jqGrid table

The issue at hand is that when using jqGrid with cellEdit:true, users are unable to select text in a cell. Once the mouse button is released, the selection resets. This limitation prevents users from copying the selected text within cells. Has anyone enco ...

The 'wrapper' property is not present in the 'ClassNameMap<never>' type in Typescript

Hey there, I've been encountering a puzzling issue in my .tsx file where it's claiming that the wrapper doesn't exist. My project involves Material UI and Typescript, and I'm relatively new to working with Typescript as well as transiti ...

Tips for continuously running a loop function until retrieving a value from an API within a cypress project

Need help looping a function to retrieve the value from an API in my Cypress project. The goal is to call the API multiple times until we receive the desired value. let otpValue = ''; const loopFunc = () => { cy.request({ method: &ap ...

What sets Angular 2 apart when it comes to utilizing [ngStyle] versus [style.attribute]?

When using Angular 2, what distinguishes the following 2 options for passing a variable value to a style? Are there advantages and disadvantages, or is it simply a matter of personal preference, or is one more adaptable/meant for specific purposes? Option ...

hitting the value of the text input

Is there a way to strike through only the first word in an input box of type text, without editing the html? I've tried using css text-decoration: line-through; but it's striking both words. Any suggestions on how to achieve this using javascript ...

Steps for allowing the cells in a Data-Table column to be edited

Is it possible to have editable cells in a column of a Data Table with the support of the plugin? ...

The ForbiddenError has struck again, this time wreaking havoc in the realms of Node.js, Express.js

I am currently adapting this GitHub sample application to utilize Express instead of KOA. However, I am encountering an Access Denied issue when the / route in Express attempts to load the index.html. What specific modifications are required in the code be ...

What is the method for reverting style properties back to their CSS defaults using Javascript?

The php-generated page contains multiple elements structured like this: <td class="defaultTDStyle" style="color:userDefinedCustomColor" id="myTDId"></td> There is a default style in place with additional styles ap ...

Error in Angular Google Maps Component: Unable to access the 'nativeElement' property as it is undefined

I am currently working on creating an autofill input for AGM. Everything seems to be going smoothly, but I encountered an error when trying to integrate the component (app-agm-input) into my app.component.html: https://i.stack.imgur.com/mDtSA.png Here is ...

Sending the array data to the component and executing it

Trying to familiarize myself with Vue2, but I am having issues with the "markers" array not updating on the map. How can I make this array reactive and ensure that the map markers change accordingly? Thanks for any assistance! views/Map.vue <template ...