When using @click as a link, it results in an

I'm attempting to create a clickable row in a datatable that functions as a link. Placing a standard <nuxt-link> within a <td> works fine, but wrapping the entire <tr> disrupts the table layout.

Next, I tried using @click with a method, which resulted in a 404 error: TypeError: Cannot read property 'nuxtLink' of undefined


<tbody class="bg-white">
    <tr
        v-for="(productCard) in productList"
        :key="productCard.acquisitionCost"
        :class="[
            'hover:bg-gray-50',
            'transition',
            'ease-in-out',
            'duration-150',
            'cursor-pointer'
        ]"
        @click="goToProduct()"
    >
        <td
            class="px-6 py-4 whitespace-no-wrap border-b border-gray-200 text-sm leading-5 text-gray-500"
        >
            {{ productCard.oVariant }}
        </td>
        <td
            class="px-6 py-4 whitespace-no-wrap border-b border-gray-200"
        >
            <div class="text-sm leading-5 text-gray-900">{{ productCard.gear }}</div>
            <div class="text-sm leading-5 text-gray-500">{{ productCard.motor }} hk</div>
        </td>
        <td
            class="px-6 py-4 whitespace-no-wrap border-b border-gray-200"
        >
            <div class="text-sm leading-5 text-gray-900">{{ productCard.fuelType }}</div>
            <div class="text-sm leading-5 text-gray-500">{{ productCard.economy }} km/l</div>
        </td>
        <td
            class="px-6 py-4 whitespace-no-wrap border-b border-gray-200"
        >
            Tom
        </td>
        <td
            class="px-6 py-4 whitespace-no-wrap border-b border-gray-200"
        >
            Tom
        </td>
        <td
            class="px-6 py-4 whitespace-no-wrap border-b border-gray-200 text-sm leading-5 text-cool-gray-700"
        >
            DKK <b>{{ productCard.monthlyPrice }},</b>- mdl.
        </td>
        <td
            class="px-6 py-4 whitespace-no-wrap border-b border-gray-200"
        >
            <nuxt-link
                :to="productCard.nuxtLink"
            >
                <svg class="h-5 w-5 text-gray-400" fill="currentColor" viewBox="0 0 20 20">
                    <path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd"/>
                </svg>
            </nuxt-link>
        </td>
    </tr>
</tbody>

methods: {
    goToProduct: function () {
        location.href = this.productCard.nuxtLink
    }
}

Any suggestions on how I can make the entire row act as a link?

Answer №1

To utilize the productCard, you should pass it to the method:

@click="goToProduct(productCard)"

Subsequently:

goToProduct: function (productCard) {
  window.location.href = productCard.nuxtLink
}

Instead of directly setting the href, consider interacting with the router for a more optimal approach.

Answer №2

To follow @Skirtle's advice, it is best to pass the productCard as a parameter and utilize it within the method:

@click="goToProduct(productCard)"

Instead of using location.href to redirect to the specified URL, it is recommended to leverage Nuxt.js functionality such as the $router instance like so:

this.$router.push({path: productCard.nuxtLink})

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

Learning to retrieve JSON data from an API using JavaScript

Greetings! I am currently facing an issue while trying to retrieve JSON data from an API. Whenever I attempt to extract data from the API, everything works smoothly except for when I try to access the distance value, which results in undefined. Any assista ...

Encountering a Console warning while working with the Material UI menu. Seeking advice on how to resolve this issue as I am integrating HTML within a text

Caution: PropType validation failed. The prop text provided to LinkMenuItem is invalid as it should be a string, not an object. Please review the render method of Menu. Below is the code snippet: var menuItems = [ // text is not valid text { route: &ap ...

Can existing servers support server side rendering?

I am currently working on building a Single Page Application (SPA) using the latest Angular framework. The SPA will involve a combination of static HTML pages, server side rendering, and possibly Nunjucks templating engine. My dilemma lies in the fact th ...

Utilize the withCredentials property in conjunction with $resource

I am looking to utilize a resource with a cookie set in the navigator. Using $http makes it simple, just need to set withCredentials to true: $http({ method: 'POST', url: url, data: user, withCredentials: true }); However, for ...

Error loading resource in Vue npm run serve: net::ERR_CONTENT_LENGTH_MISMATCH

I am encountering the following error message in Google Chrome console: Failed to load resource: net::ERR_CONTENT_LENGTH_MISMATCH chunk-vendors.js:1 This results in a blank page when attempting to load a Vue development page initiated with: user@ubuntu:~# ...

What is the process for reinserting a list item into the nested elements?

Looking for help with manipulating a menu in HTML? I have a menu that I want to modify by removing and adding list items. While I've had success removing items, I'm struggling to properly use the add method. Here's an example of what my menu ...

Troubleshooting JavaScript Oscilloscope: resolving audio playback problems

I am exploring JavaScript for the first time and came across an interesting oscilloscope example on this GitHub page. It seemed quite easy to follow initially, but I am facing an issue with audio playback. Although the HTML5 audio element loads the audio f ...

Unable to display tags with /xoxco/jQuery-Tags-Input

I'm experimenting with the tagsinput plugin in a textarea located within a div that is loaded using the jquery dialog plugin. The specific plugin I am using is /xoxco/jQuery-Tags-Input. After checking that the textarea element is ready, I noticed th ...

The function window.open when using the target '_blank' will launch a fresh browser window

I'm attempting to open a link in a new browser tab (not a new window). When I place a link on the page like this: <a href="#" onclick="window.open('http://www.google.com', '_blank');"> Click Here</a> when a user clic ...

Learn a valuable trick to activate CSS animation using a button - simply click on the button and watch the animation start over each time

I already know how to do this once. However, I would like the animation to restart or begin again when the user clicks on it a second time. Here is what I have: function animation() { document.getElementById('ExampleButton').className = &apo ...

What is the best way to implement this component into my vue.js project?

I am having trouble integrating this vue component into my app as it is not loading properly. You can find the source of the component here. The following warnings are showing up: Unresolved function or method isNumeric() at line 35 Unused parameter e at ...

Steps for adding a border to kendo grid row hover

One of my tasks involved creating a grid using Kendo, and I wanted to display borders on a grid row when the cursor hovers over it. I attempted the following code snippet: .k-grid > table > tbody > tr:hover, .k-grid-content > table > tbody ...

React components to separate static PDF pages

I have successfully implemented a feature in my React App that allows me to export a long page in PDF format using html2canvas and jsPDF. The code snippet for exporting the page is as follows: html2canvas(document.body).then((canvas) => { var img ...

AngularJS: Issue with watching arrays and deep $watch functionality

I'm having trouble using the $watch function in AngularJS with an array of boolean values. I want to display a message when there's a change in the array, but it's not working as expected. Check out this code example where the array values ...

How can the navbar be kept persistent in vue-router?

Coming from a background in React, I am currently learning Vue and have encountered an issue regarding the persistence of a navbar with router links. My navigation bar component works fine, but it keeps reloading whenever I navigate to a different route. I ...

Navigating with firebase authentication and angular routing

Currently, I am in the process of developing an ionic app using version 4. For this project, I have decided to utilize firestore as my database and implement firebase-authentication for user login and registration functionalities. However, I have encounter ...

Implementing Jquery tabs into the code causes the vertical auto-scroll to smoothly glide beyond anchor points

I recently implemented a smooth autoscroll feature on my webpage using the CSS-Tricks code found here: http://css-tricks.com/snippets/jquery/smooth-scrolling/ Everything was working perfectly until I added jQuery tabs to display some of the content. Now, ...

Tips for creating a clickable textbox

Does anyone know how to make a textbox clickable even when it is set as readonly. I'm looking for a way to make my textboxes clickable like buttons because I have some future plans for them. <input type="text" readonly value="Click me" id="clickm ...

How can one efficiently update numerous data properties in a Vue.js application?

My information is as follows: data() { return { monday_start: '', monday_end: '', tuesday_start: '', tuesday_end: '', wednesday_start: '', ...

What could be causing my Vue JS and Laravel download feature to produce corrupt files?

Currently, I'm working on a download feature that allows users to download files of all types that they have uploaded. Although the downloading feature is functioning properly - files are appearing in my downloads folder and the file type is recognize ...