Having trouble with VueJS Router-link not redirecting to the correct view?

I am facing an issue with the router link. When I use it in a view, it works perfectly fine as I have a clickable link. However, when I include a router link in a component nested within a view, the router link stops working - all I want is to link to the details of a project.

This is how it looks in my work (

resources/js/views/HomepageView.vue
)

 <router-link :to="{ name: 'projects.show', params: { slug: slideshowProject.slug }}">
    <a href="#" class="btn-secondary">See
        Campaign</a>
</router-link>

But this doesn't work (

resources/js/components/UserProject.vue
)

<router-link :to="{ name: 'projects.show', params: { slug: project.slug }}">
    <h4>{‌{ project.title}}</h4>
</router-link>

Here's the script part of the page :

<script>
    export default {
        name: "user-projects",
        data() {
            return {
                projects: null
            }
        },
        mounted() {
            this.getUserProject()
        },

        methods: {
            getUserProject() {
                this.$store.dispatch('getUserProjects', {
                    limit: 2
                }).then(response => {
                    this.projects = response.data.data;
                })
            }
        },
    }
</script>

This is from my router.js file

import Homepage from '../views/frontend/HomepageView';
import ProjectDetails from '../views/frontend/ProjectDetailsView';
import LoginView from '../views/frontend/LoginView';
import RegisterView from '../views/frontend/RegisterView';
import DashboardIndexView from '../views/dashboard/DashboardIndexView';

export const routes = [
    {
        path: '',
        name: 'homepage',
        component: Homepage
    },
    {
        path: '/login',
        name: 'frontend-login',
        component: LoginView
    },
    {
        path: '/projects/:slug',
        name: 'projects.show',
        component: ProjectDetails
    },
    {
        path: '/register',
        name: 'frontend-register',
        component: RegisterView
    },
    {
        path: '/dashboard',
        name: 'dashboard-index',
        component: DashboardIndexView
    }
];

I'm unsure where I may be going wrong :/

Answer №1

To ensure the router link is rendered correctly, it's important to verify that the slug is not empty beforehand, as shown in the example below:

<router-link v-if="project.slug" to="{ name: 'projects.show', params: { slug: project.slug }}">
    <h4>{‌{ project.title}}</h4>
</router-link>

I'm uncertain about why the slug might be empty in this context.

Answer №2

Thanks to Jom, I was able to identify the issue with my empty "slug". Upon checking the console, a warning message confirmed this.

[vue-router] missing param for named route "projects.show": Expected "slug" to be defined

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

Please refrain from displaying the POST response in Express

I have a simple Express API setup: app.get('/example', function(req, res) { if (req.body.messageid == 1) { res.send({message: "Message"}); } } The API returns a message to be displayed on an HTML page. To display the message, I created ...

Determining if the device orientation matches the given coordinates through a logical process

Exploring the capabilities of browser API's related to geolocation and device orientation. I'm wondering, if I have information on someone's orientation (like a compass angle from 0 to 360 degrees), is it feasible to determine if they are f ...

What is the process for establishing Many-to-Many connections with personalized field titles using Bookshelf.js?

I am interested in setting up a many-to-many relationship using Bookshelf.js and I would like to customize the names for the foreign key columns. Additionally, I want to have access to the helper table in Bookshelf just like in the example below: var Phys ...

Tips for extracting HTML tags directly from JSON data

In my dataset, I have a JSON illustration [{ "Field1": "<header class=\"main-header dark-bg\">\n\t\t<div class=\"row\">\n\t\t\t\t<div class=\"col-xl-3\">\n< ...

The page is constantly updating on its own

In my React app, the App component checks for a token in local storage upon loading. If a token is found, it is verified. If the token is valid, the user is directed to the dashboard; otherwise, they are taken to the login page. The issue I am facing is ...

Is there a way to alter the camera's focal point in THREEJS to focus on a specific object already in the scene?

Currently, I am working with a scene extracted from a THREEJS example (https://threejs.org/examples/webgl_loader_fbx.html) that involves importing and displaying a threejs.json scene. The rendering of the scene works perfectly; I have a grid in place with ...

Utilizing client-side storage within a React project

As part of my React challenge tracking app development, I am looking to implement a feature where users can click on a challenge button, approve it, and then save the chosen challenge name to local storage. Later, this saved challenge will be displayed in ...

"Embracing Progressive Enhancement through Node/Express routing and the innovative HIJAX Pattern. Exciting

There may be mixed reactions to this question, but I am curious about the compatibility of using progressive enhancement, specifically the HIJAX pattern (AJAX applied with P.E.), alongside Node routing middleware like Express. Is it feasible to incorporate ...

Angular 2 component stays static despite changes in route parameters

So, I am in need of a way to refresh my component after the URL parameter's id has been altered. The following code is from my player.component.ts: import {Component, OnInit, AfterViewInit} from '@angular/core'; import {ActivatedRoute, Rout ...

What is the process for retrieving an object from a node.js server using JSON.stringify() in a JavaScript file?

Looking to organize my code, I want to separate the JavaScript from my page and link it through a file instead of having it embedded within script tags. The issue arises when I receive a "SyntaxError: expected expression, got '<' " in Firefox ...

Initiate an AJAX request to the server upon beforeunload event

It seems that starting with Firefox version 4, binding the window jQuery object to beforeunload is no longer effective. I want to send an AJAX post request to delete data from my server's memcache. When I refresh the only open tab, I can observe tha ...

Cracking the code of the @ symbol within this particular context

https://github.com/react-dnd/react-dnd/blob/master/examples/04%20Sortable/Simple/Card.js I'm trying to comprehend the significance of the @ symbol in this example. This is meant to be a straightforward drag and drop demonstration, but the implementa ...

The method to eliminate the default active class using AngularJS when clicking on different links

Here is the scenario: The first link is initially active and highlighted with the active class when the page loads. <a class="button active" ng-click="$parent.Filter = ''" ng-class="{ 'active': Filter === '' }"> Test l ...

Steps for including a header and footer with page numbers in an HTML page without disrupting the rest of the content when printing

When I print a page with a table that spans multiple pages, I want to ensure that my header and footer are included on every page. The code I've tried so far has treated the entire content as one continuous page. How can I achieve this? ...

The IMDB API is throwing a GraphQL error that says, "ClientError: Unable to retrieve information on the field 'Image' for the type 'MainSearchEntity'."

Seeking suggestions for the top 10 movies related to 'Africa' using the IMDB API demo available here. In my query, I am looking for movie id, title, poster image, and filming location. However, I encounter an error message 'ClientError: Ca ...

Using a JavaScript onclick function to retrieve specific elements within the document

I'm attempting to extract the elements from the source that was clicked on, but for some reason it's not functioning correctly. Check out my JSFIDDLE Here's the HTML: <span class="populate" onclick="populate();" href="?id=1">Hello&l ...

Does the AJAX load more feature duplicate the existing data?

I've successfully implemented a PHP code that generates JSON data from WordPress posts in the database. By using AJAX, I'm able to load this JSON on my HTML page without any issues. Now, I want to enhance this functionality by adding a "Load Mo ...

Exploring the Concept of Event Bubbling in ReactJS

Having recently delved into React.js, I've encountered a roadblock that has persisted for the past three days despite my attempts at various solutions. The issue revolves around two functions in my parent component named checkout: handleSeleteAddress ...

Automatically appending textarea value in HTML tag upon form submission via JQuery

When a form is submitted through JQuery, the HTML tag automatically adds textarea value. This is my JQuery code: $('#calling').click(function() { $('#myform').submit(); }); Within my form, there is a textarea element: <textar ...

I am interested in developing a program using Javascript or JQuery that can effectively capture the anchor text within <a></a> link tags

I want to automatically capture the link on my website and create a system where, when users click on a specific link, it opens the captured link in a new tab without requiring browser permission and replaces the current tab with a different link. For exa ...