Utilizing Vue route parameters for making Axios GET requests

I'm currently working on my first vue project and facing an issue with passing route parameters to an axios get request. The code below represents the page that is rendered after a user clicks on a dynamic link within a table of tests:

<template>
  <v-app>

    <app-navbar />

    <v-main>
        <h3>test {{$route.params.name}}, {{$route.query.status}},{{$route.query.tag}}</h3>
        <h3>{{items}}</h3>
    </v-main>
  </v-app>
</template>

<script>
import appNavbar from '../../../components/appNavbar.vue';
import axios from "axios";
export default {
  components : {appNavbar},
  name: "App",
  data() {
    return {
      items: [],
    };
  },
  async created() {
    try {
      const res = await axios.get(`http://localhost:3004/tests`,{ params: $route.params.name });
      this.items = res.data;
    } catch (error) {
      console.log(error);
    }
  },
};
</script>

<style lang="scss" scoped>

</style>

Does anyone know how I can successfully pass the route parameters to the axios get function?

Answer №1

Make sure to have this.$route.params.name within the script section.

const response = await axios.get(`http://localhost:3004/tests`,{ params: this.$route.params.name });

Answer №2

Router.js contains an array of routes with objects like { path: 'path/:name', name: 'Name', component: ComponentName }

Answer №3

Utilizing the solution provided by @Nitheesh, I simply needed to specify the parameter name for it to function flawlessly. Here's the revised solution:

const result = await axios.get(`http://localhost:3004/tests`,{ params: {name:this.$route.params.name} });

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

Encountering a TypeError in semantic-ui-react Menu: instance.render function not found

As a React novice, I have been working on a project that involves React for some time. However, I had not yet delved into dealing with packages and dependencies. It seems like my current issue is related to this. The problem emerged in a project where I w ...

The concept of a callback function is not applicable within the context of MongoDB in Node.js

I am encountering an issue while validating the existence of an email or username in my MongoDB users collection within Node.js using my User Model. Whenever I attempt to perform this validation, I receive an error stating callback is not a function. This ...

Sorry, but we couldn't locate the page you were looking for. The robots.txt file seems

Hey there everyone, I'm currently working with Nuxt3 and encountering an error when trying to access the robot.txt file. Here's the Robot.txt module that I've integrated. Within my nuxt.config.ts file: export default defineNuxtConfig({ ...

Unpacking and reassigning variables in Vue.js 3 using TypeScript

I am working with a component that has input parameters, and I am experimenting with using destructuring assignment on the properties object to reassign variables with different names: <script setup lang="ts"> const { modelValue: isSelected ...

Is there a way to restrict a user to selecting just one checkbox in a bootstrap checkbox group?

I am having trouble implementing bootstrap checkboxes in my code. I want the user to be able to select only one checkbox at a time, with the others becoming unchecked automatically. Can someone please advise me on what I need to add to my code? Here is a ...

Replacing npm package dependency

I came across this post: How can I change nested NPM dependency versions? Unfortunately, the solution provided did not work for me. I am attempting to modify a package to utilize a different version of a specific dependency instead of the one it currentl ...

Vue3: Implementing function overrides using the Composition API

Consider a composable function as shown below: // composable/useShareComp.ts export function useShareComp() { const toto = "hey"; const tata = ref(0); function utilMethod() { console.log("Util Méthod !!"); } function mai ...

I am attempting to link my Firebase real-time database with Cloud Firestore, but I am encountering import errors in the process

I am currently working on enhancing the online functionality of my chat app by implementing a presence system using Firebase Realtime Database. Here is the code snippet that I have created for this purpose: db refers to Firestore and dbt refers to the Rea ...

Opening up a method or property for external access beyond the Vue framework

I recently discovered in Vue's documentation that it is possible to modify the data of a Vue instance created with new Vue directly from the console. However, as my application grew and I delved deeper into Vue, I encountered issues with this approach ...

Is there a way to ensure the functionality of this code without exposing my API keys? I am currently developing an application using Node.js, Angular, and Express

I have come across an issue with my code where process.env is not working within a specific function. I need to find a way to call process.env without displaying my keys. If I don't add my keys, the API call won't function properly. What options ...

Having trouble with your JSON parsing in JavaScript when coming from PHP?

Being new to PHP and AJAX, I am encountering an issue where the JSON returned from the server is not parsable. Below is my initial PHP code snippet: <?php header("Access-Control-Allow-Origin: *"); header('Content-type: application/json'); r ...

The section element cannot be used as a <Route> component. Every child component of <Routes> must be a <Route> component

I recently completed a React course on Udemy and encountered an issue with integrating register and login components into the container class. The course used an older version of react-router-dom, so I decided to upgrade to v6 react router dom. While makin ...

Looking for assistance with flipping the order of words in a string?

How do I achieve the desired output by passing the specified string as an argument to a function? input: "Reverse this line" output: "esreveR siht enil" This is my implementation function reverseWords(string) { var wordArray = string.split(" ...

Trouble viewing Geojson map on website

I'm having trouble generating a map. I am currently working with one of the geojson files provided by The New York Times ("census_tracts_2010.geojson") from this link ( https://github.com/dwillis/nyc-maps ). If someone could take a look at my code be ...

Problem with Vue JS Hooper Image Slider (integrated into NuxtJS application)

Issue with Loading Images I encountered a problem while using the carousel feature in this GitHub project (). The images in the carousel do not display on the first load; instead, a loader animation image appears. However, upon refreshing the page, the ca ...

Tips to prevent browser from freezing while creating a large number of HTML elements

I am currently utilizing Selection.js to develop a customizable grid on my website. To make this work effectively, I need a specific number of div elements to establish the selectable area. In my scenario, I generate all the divs using a for loop and then ...

Display the answer in Vue.js upon submission

I'm struggling to display the correct answer in the answer block. When I use {correctanswer(question.responses)} I keep getting undefined as I loop through the responses. <div class="ques_block" v-for="(question, index) in quiz.questions"> ...

Vue shallow mounting not effective, issues with stubs

I am currently working on writing a Jest test for a Vue component that includes rendering a subcomponent. Here is the structure of the component: <template> <div class="add-to-cart-position"> <a :href="item.url"> <picture- ...

After making a selection from the list, I would like to automatically close the select menu

<div class="topnav__menu"> <button class=" topnav__button topnav__button--has-arrow topnav__menu-button " type="button" id="hideselectOptio ...

Using iTextSharp in .Net to convert the action result into a downloadable pdf file via ajax

I have been successfully using iTextSharp to convert a razor view into a downloadable PDF via a C# controller. While the current implementation is functioning perfectly, I am seeking a way to transmit a model from a view to the PDF controller and enable th ...