Tracking and managing user clicks on external links within a vue.js application

I am currently working on a web application that retrieves data from a CMS. Utilizing the Vue-Router in 'history' mode, I need to address content fetched from the API which may include links. My goal is to manage specific links using the router while allowing all other links to function normally.

Is this type of functionality feasible? I attempted to substitute the <a> tags within the content with router-links, but encountered issues as I am utilizing v-html to display the content. In jQuery, I would have monitored events on all <a> elements and evaluated the links accordingly. However, I am uncertain how to accomplish this elegantly in Vue.

Answer №1

It seems like you are looking to compile components from dynamic content that you are binding. I faced a similar issue before and found a helpful solution on the vue forum. You can check it out here: https://forum.vuejs.org/t/dynamically-compile-router-link/7410/3

LinusBorg suggested using a dynamic computed component with a transformer method to parse links and convert them into router links:

computed: {
  transformed() {
    return {
      template: transformer(...),
      props: this.$options.props
    }
  }
}

You can then dynamically bind it in the template like this:

<component v-bind:is="transformed" v-bind="$props"/>

After experimenting with this approach, I was able to achieve my desired outcome. The transformer method replaced tags with router links and returned the modified string as the new template for the "component."

I hope this information proves useful to you.

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

Avoiding redirection in Django template form

Currently, I am facing a challenge with my application that involves rendering a Django Template model form where images are referenced from another model. To manage the addition and deletion of images for this other model, I have implemented a button wit ...

Guide to converting a log string into JSON using Javascript

I encountered a console log that appears as follows: 2021-01-06T09:06:05.541212726Z D saveGarment: Function execution took 736 ms, finished with status code: 204 2021-01-06T09:06:10.844901031Z D saveGarment: Function execution started 2021-01-06T09:06:16.1 ...

Getting some clarity on how to structure a project using Node.js, Express.js, and React.js

I am in the process of developing a website for online shopping purposes, essentially an e-commerce platform. However, I am facing a dilemma where create-react-app sets up its own Node.js server to communicate with my backend (which handles MySQL queries) ...

steps to eliminate a cookie upon second click

I have successfully implemented a feature where the color of a button is stored in a cookie, so that when the button is clicked, the CSS color remains even after page refresh. However, I am struggling to figure out how to destroy the cookie after the secon ...

What could be causing my state not to change in Nextjs even though I followed the quick start guide for Easy Peasy?

I recently encountered an issue while trying to implement easy peasy for global state management in my nextjs app. The problem I faced was that the state would only update when I changed pages, which seemed odd. To better understand what was going on, I de ...

What could be the reason my code isn't successfully performing addition within the input field?

As a novice, I am practicing by attempting to retrieve a number from a text field, prompting the user to click a button that adds 2 to that number, and then displaying the result through HTML. However, I keep encountering an issue where NaN is returned whe ...

When executed, the Node application successfully compiles

I have a TypeScript application that runs smoothly in development mode using ts-node. However, after building the application, I encounter some unexpected warnings and errors. This is my tsconfig.json: { "compilerOptions": { "incremen ...

What is causing the default switch to constantly loop in repetition?

Currently, I am working on a Discord bot using discord.js and utilizing a switch statement. However, I am encountering an issue where the "default:" case keeps repeating itself every time any other case is executed. I have already investigated for cases w ...

Encountering an error message stating "Unable to read property 'map' of undefined while attempting to create drag and drop cards

I have incorporated the library available at: https://github.com/clauderic/react-sortable-hoc The desired effect that I am aiming for can be seen in this example: https://i.stack.imgur.com/WGQfT.jpg You can view a demo of my implementation here: https:// ...

Ways to retrieve my PHP output and display it on a page using Ajax

I'm struggling to combine the functionalities of a PHP script and Ajax on my website. I want to update content without reloading the page but facing issues. Although I can send input data through Ajax, I'm unable to retrieve the results effectiv ...

The GET request is returning a null array instead of the expected array contents

I am new to this, but I am trying to understand why my GET request is returning an empty array even though I am sure that the Mongo database collection is not empty. Each word form in the WordForm collection has a "lexicalform" key that refers to a Lexicon ...

Panel that collapses and increments its ID each time within my loop

I have a Collapsible Panel with this header, <div id="CollapsiblePanel1" class="CollapsiblePanel"> <div class="CollapsiblePanelTab" tabindex="0">Comments</div> <div class="CollapsiblePanelContent"> Content &l ...

Upload an image converted to `toDataURL` to the server

I've been attempting to integrate the signature_pad library, found at signature_pad, but I am struggling to grasp its functionality. Can someone guide me on how to retrieve an image value and send it to my server? Edit: I have experimented with dec ...

Moving information from two modules to the service (Angular 2)

Recently in my Angular2 project, I created two components (users.component and tasks.component) that need to pass data to a service for processing before sending it to the parent component. Code snippet from users.component.ts: Import { Component } fro ...

The value being passed to the Child Component is only updating once

Within my Vue app, the parent component contains a list of months structured as shown below: https://i.stack.imgur.com/B9XQK.png Upon clicking on any month from the list, I aim to transfer its corresponding id to the child component named timeComponent. ...

AngularJS: incorporating modules across distinct controllers

Imagine we have two distinct AngularJS controllers housed in separate files that are both referenced in an HTML document. Here's how it looks: //controller1.js "use strict"; var someApp = angular.module('MYAPP'); //var someApp = angular.mod ...

Can you explain the significance of response.on in Node.js?

Currently facing an issue with a Node.js http request. If I can't figure it out, there's a bigger question that I'll address later. I've been working on some code and came across 'response.on', but I'm not entirely sure ...

What steps should I take to show a particular set of data upon selecting a checkbox component?

I have a table with a column named status, which can be in progress, pending, or dispensed. My goal is to filter the data based on the checkbox that is selected above the table. For instance, if I check the "pending" checkbox, only the data with the pendi ...

Tips for displaying videos with sound when the autoplay has been successfully implemented, but the audio is not working

I've been working on creating a video player with autoplay functionality, but I'm facing an issue with the sound not playing. Here's what I currently have in my code, but it's not behaving as expected: var myaudio = docum ...

Verify that a minimum of one checkbox has been ticked according to JavaScript guidelines, with appropriate notifications in case

Need help with displaying a message when no checkbox is checked. Currently implementing JavaScript rules and messages <form method="post" enctype="multipart/form-data name="english_registration_form" id="english_registration_form" > <div cl ...