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

What could be the reason for XMLHttpRequest's readystate being something other than 4?

Currently delving into the realm of AJAX for the first time, and endeavoring to construct a basic application that computes the required grade on a final exam based on past assessment results. The user keys in their grades into the form, subsequent to whi ...

Tips for closing 2 models in a React application

Currently, I am working on a project using ReactJs. One of the functionalities I am implementing is generating a user's gallery when the user clicks on their avatar. To achieve this, I am utilizing the react-grid-gallery package along with semantic ui ...

Simplify code by eliminating uuid references

Greetings! I am currently working with some code that is generated within a JSP tag and utilizes the jQuery data function to connect data with a div element. In order to link the jQuery script with the div on the webpage, I have employed a UUID. However, ...

Create a visual representation of an image by sketching a detailed line profile using HTML5's

I am attempting to create an intensity profile for an image, using the x-axis as the line's length on the image and the y-axis as the intensity values along the length of the line. How can I achieve this on an HTML5 canvas? I have tried the code below ...

Is there a way to ensure that clicking a link_to only opens a partial once?

These are the files I am working with: _comment.haml %div.comment{ :id => "comment-#{comment.id}" } %hr - if current_user && current_user.id == comment.user_id || current_user && current_user.id == reel_user = link_to " ...

Restricting the drop zone for a jqueryui sortable element

In my HTML code, I have a list of elements inside a ul tag. The last li element is supposed to create new items. I used jqueryui's sortable() function to make the ul sortable, but excluded the last li element from being draggable. However, other items ...

The functionality of AngularJS ng-model seems to be disrupted when it is set within a directive

Is it possible to automatically generate the ng-model of an input based on the name of the input itself? This requirement arises from using Html.TextBoxFor in MVC, which creates the necessary name for binding the input to the server-side model. To streamli ...

Changing the shading of an arc in d3.js: Tips and tricks

I've been attempting to create a gauge with a needle that changes the background color of the ring for the past few days. My goal is to dynamically update the colors of an arc within a gauge I developed in d3.js This is my current gauge setup with t ...

The method this.textSize does not exist

I encountered this issue : Uncaught (in promise) TypeError: this.textSize is not a function when using this.textSize. If I just use textSize, it prompts me that it is undefined. Interestingly, these lines of code are located in the same file and appear ...

Only line breaks are permitted in Mustache.js, all other HTML characters are escaped

After a user submits input, I am generating comments and displaying them using Mustache.js. When it comes to rendering the comments, I have found that replacing user-entered line breaks (\n) with <br/> allows them to display as HTML breaks. To ...

ng-class: Issue detected - The symbol '-' is positioned at column {2}

I'm having an issue with ng-class. I need to add a disabled state class if the role has admin privileges, but I keep getting an error. Here is the HTML: <label class="toggle modal-label-box" ng-class="{state-disabled: checkCanModify()}"> &l ...

Steps for invoking the next() function within the beforeRouterEnter guard

I've been grappling with this issue for countless hours now. I'm currently using Vue3 My goal is to implement a beforeRouterEnter guard in order to check the user's role upon login and direct them to the appropriate route accordingly. Once ...

Unable to invoke the Socket.on() function in NodeJS

I have set up a PHP web app and I am trying to integrate nodeJS into it. I followed a tutorial on nodeJS and got it working fine on localhost:3000. But now, I want to run it on a specific URL like localhost/final/chat/chat_index.html. To achieve this, here ...

jqGrid is failing to display basic JSON data properly

As a newcomer to Jquery and Json, I am struggling with binding a JSON object from a RESTful Webservice written in WCF to jqGrid. Despite saving the JSON object as a static file and attempting to bind it to the grid, I realized that the issue does not lie w ...

Radio buttons with multiple levels

Looking to implement a unique two-level radio button feature for a specific option only. Currently, I have written a logic that will display additional radio buttons under the 'Spring' option. However, the issue is that when it's selected, t ...

While it is possible to filter by friend.brand.id within friends in Angular, unfortunately the || undefined trick cannot be used to reset the filter when it is NULL

In the futuristic world of 2075, immortal humans have subjugated mortal humans. There exists a unique store where mortal friends are sold, and efforts are being made to develop an app that allows customers to manage their friends based on their favorite br ...

Angular: Maximizing Input and Output

I'm having trouble with the function displaying within the input field. My goal is to simply allow the user to enter a name and have it displayed back to them. HTML: <div ng-app = "mainApp" ng-controller = "studentController"> <tr> < ...

Sequential selection in Node-firebird is non-functional

I've been testing some simple code sequentially using the Firebird library called "node-firebird." Despite having multiple rows, it only returns 1 row every time. exports.sequentially = (select, db_con_options) => { Firebird.attach(db_con_options, ...

The clone() function in jQuery causes the properties of the original element to become distorted

Seeking help with a curious issue I'm encountering. After selecting a radio button and cloning it, the original radio button becomes unchecked while the cloned one behaves as expected. Any insights into why this might be happening would be greatly app ...

Ways to verify the presence of an empty array object

I'm trying to determine whether all arrays inside an object are empty. To illustrate, consider the following object: var obj = { arr1: [0, 1, 2], arr2: [1, 2, 3], arr3: [2, 3, 4] }; After pop()ing values from the arrays within the object, I ...