Create a loop in Vue.js 3 without the need for a query

Can someone help me understand how to fetch data using a loop in Vue.js version 3 when working with queries? I am trying to retrieve an object based on its id, which is obtained from the URL. However, I seem to be facing some difficulties. Any guidance would be greatly appreciated. Thank you.

vue routing

{
    path:'/faq/:idQuestions',
    name:'FaqQuestion',
    component:() => import('../components/admin/faq/FaqQuestion.vue')
},
<template>
    <div>
        <h2 class="title-faq">Questions fréquentes</h2>
        <div class="list-faq"& gt;
            <router-link @click="getQuestion" class="link-vehicule" :to=" 
  {name:'FaqQuestion', params:{idQuestions:1}}"><p class="list-faq_question">test 1</p></router-link>
            <router-link class="link-vehicule" :to="{name:'FaqQuestion', params:{idQuestions:2}}"><p class="list-faq_question">test 2</p></router-link>
            <router-link class="link-vehicule" :to="{name:'FaqQuestion', params:{idQuestions:3}}"><p class="list-faq_question">test 3</p></router-link>
<template>
    <div>
        <div v-for="faq in faqQuestion" :key="faq.id">
                <p>{{faq.id }}</p>
                <h1 >{{ faq.title }}</h1>
                <p>{{ faq.question }}</p>  
        </div>
    </div>
</template>

<script>
export default {
    name:"FaqQuestion",
    data(){
        return{
            idTest: 2,
            question:[],
            //id:2,
            faqQuestion:[
              {
                        id: 1,
                        title:"New-title-1",
                        question: "test 1"               
                },
                {              
                        id:2,
                        title:"new-title 2",
                        question: "test 2"              
                },
                {   
                        id:3,
                        title:"new title 3",
                        question: "test 3"        
                }
            ]
        }
    },
   beforeMount(){
         const url = window.location.href;
        this.id = url.split("/").slice(-1)[0];
       console.log(this.id)
    }
}
</script>

Answer №1

To access the route parameter in your template, use the following syntax:

{$route.params.idValue}

Alternatively, you can access it within component methods using this.$route.params.idValue.

If you need to find specific data based on the route parameter, you can utilize Array.find() like so:

this.dataArray.find(item => item.id === this.$route.params.idValue)

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

When a button is clicked, the event is not triggered within a FirefoxOS application

Despite functioning perfectly on Firefox for desktop, my application encounters issues when tested on my ZTE Open C (Firefox OS 1.3). Pressing the button does not trigger any action, while onmouseup and onclick events work flawlessly. Even attempts to bin ...

Can the hasClass() function be applied to querySelectorAll() in JavaScript?

As a beginner in javascript and jquery, I recently attempted to manipulate classes using the following code snippet: if ($(".head").hasClass("collapsed")) { $(".fas").addClass("fa-chevron-down"); $(".fas&qu ...

Transmit responses from PHP script

I am in the process of creating a signup form where, upon clicking the submit button, all form data is sent to a PHP file for validation. My goal is to display an error message next to each input field if there are multiple errors. How can I achieve this ...

Using Jquery's getJson method, we can easily fetch and retrieve the appropriate JSON string by

I have the following code snippet at the beginning of the JSON url: if(isset($_GET['template']) && $_GET['template']=="blue") { $inifile_path="ctr/subthemes/blue/"; } else { $inifile_path="ctr/subthemes/fresh-n-clean/"; } Addi ...

Some places may not have detailed information available when using the Google Places API

Greetings I am currently in the process of developing a page on my website that utilizes the Google Places API along with the user's geographical coordinates (latitude, longitude) to locate nearby restaurants. In my code, I have implemented a functio ...

Bring in Bootstrap and the Carousel plugin using Webpack Encore

Currently, I am tackling an issue within my Symfony project that involves Webpack Encore and the loading of Bootstrap and the Carousel plugin. The problem may stem from how I import Bootstrap, as it seems to partially work when I import the file like this ...

Remove the initial DIV element from the HTML code

I have created a unique chat interface by combining JScript, php, and jquery. The chat messages are saved in an HTML document that gets displayed in the text area. Each user input message is contained within its individual div tag: <div>Message</ ...

Planes in Three.js that are overflowing may have their opacity adjusted

My current challenge involves aligning two planes in the same (or closest possible) position. When two planes made of different materials and colors occupy the same space, depending on the camera angle and view perspective, the front plane's opacity ...

Preview and crop your image before uploading

Currently, I am working on developing a form that will enable administrators to upload an image. The aim is to allow them to preview the image before uploading it, displaying it at a specific size and providing the option to click on the image to open an i ...

VueJS - Error: Module not found: Unable to locate '@babel/runtime/regenerator'

I am encountering a error with my project: Module not found: Error: Can't resolve '@babel/runtime/regenerator' in 'C:\wamp64\www\project-dev\modules\Gallery\Resources\Assets\Backend\Views& ...

Begin a new countdown timer upon clicking the next button

Currently, I am developing a bid auction website and I have implemented a countdown timer script. The timer works perfectly on the initial window load. However, when I click on the restart button, it fails to reset the countdown timer to a new value. < ...

The detected coordinates are offset from the location of the mouse click

Seeking guidance: I need advice on an issue that arises when clicking on the second tooth from right to left, causing the upper teeth to be colored instead: https://i.sstatic.net/czzmc.png Below is a step-by-step explanation of what the code does: 1) T ...

Click on a button to send the React Router path as a parameter in

I've got a React form with a submission button like this: <Link className="btn btn-secondary btn-width-200 search-submit" to={{pathname: '/booking/search', query: this.state.filters}}> Search </Link> Within the ...

OpenMP and the bottleneck of parallel for loops

I am attempting to develop a C++ code using openMP for parallel processing in VS2010. While the program initially runs smoothly and efficiently, I encounter a slowdown at certain time steps, such as i = 211 and i = 316. This intermittent slowing down persi ...

Adding Data to a Database Using Ajax with PHP

I am trying to use Ajax to insert data into a database, but I keep encountering this error: Uncaught ReferenceError: insertData is not defined at HTMLInputElement.onclick (Modal.php:105) Error screenshot: The HTML and JS for this functionality are in ...

How to use the route.navigate() method in Angular 9 to open a URL in a new tab with a query string

When a button is clicked within a table in our application, I have to open a new tab with details of a specific record from the table. Currently, the code I am using navigates to a new URL and uses resolvers to fetch data from the backend on the new page. ...

Error: The 'then' method cannot be invoked on an undefined object in the Parse.File.save function

I am currently utilizing the Javascript API for parse.com within an Angular JS application. My goal is to successfully save or update the user's profile picture. Below is the code snippet that I have been working with: Some HTML . . . <input type ...

Building nested components with Vue.js involves creating a complex routing structure within the architecture

Utilizing vue.js for my administration app, I aim to create a highly modular UI architecture. Therefore, I have structured and enclosed the Header, Body, Sidebar, and Main in single file components as illustrated below. Tree App - Header - dynamic cont ...

AngularJS: Validators in Controller do not directly bind with HTML elements

The in-line validations on the HTML side are functioning properly, but when I utilize functions in my Controller (specifically ingresarNuevoEmpleador and limpiarNuevoEmpleador), the $invalid value is always true. Additionally, $setPristine does not seem to ...

AngularJS Expandable Table - Unlocking Limitless Possibilities

Take a look at the code snippet below: <tbody> <tr ng-repeat-start="ticket in tickets"> <td> <button ng-if="ticket.expanded" ng-click="ticket.expanded = false">-</button> <button ng-if="!t ...