Obtain the complete path in Vue router by utilizing nested routes

After creating nested routes for Vue Router, I encountered a problem while using the routes to generate a navigation menu. Currently, I am using route.path in 'router-link :to=' which only gives me a part of the path. I want to include the absolute path that includes all parent paths as well. Is there a way to achieve this? I attempted to iterate over all routes and set a property that would include parent paths, but I couldn't come up with a functional solution.

{ path:'/meteorologija', name:'meteorologija', component:()=>import('../pages/BlogPage'), children:[
        { 
            path:'/meteorolosko-bdenje', name:'meteorolosko bdenje', component:()=>import('../pages/BlogPage'), 
            children:[
                { path:'/aktuelni-podaci', name:'aktuelni podaci', component:()=>import('../pages/BlogPage'),
                    children:[
                        { path:'/podaci', name:'Podaci', component:()=>import('../pages/BlogPage') },
                        { path:'/trenutne-temerature', name:'Trenutne temperature', component:()=>import('../pages/BlogPage') },  
                        { path:'/ekstremne-temperature', name:'Ekstremne Temperature', component:()=>import('../pages/BlogPage') },  
                        { path:'/pritisak', name:'Pritisak', component:()=>import('../pages/BlogPage') },  
                        { path:'/vjetar', name:'Vjetar', component:()=>import('../pages/BlogPage') },  
                        { path:'/kolicina-padavina', name:'Kolicina padavina', component:()=>import('../pages/BlogPage') },  
                        { path:'/radarska-slika', name:'Radarska Slika', component:()=>import('../pages/BlogPage') },  


                    ] 
                },

I'm currently working on a function that will retrieve the path of deeply nested objects, incorporating all parent object paths:

 t(routes, path={}){
       routes.forEach((r,i)=>{
          if(r.children){
              // path+= r.path;
              this.t(r.children, path);
          }
       });

Answer №1

To access them in your component, you can use this code snippet: this.$route.path.matched. This will provide you with an array of the current matched routes. For example, if you are on the path

/meteorologija/meteorolosko-bdenje/aktuelni-podaci/podaci
, it will return something like this along with additional information:

[
    { path:'/meteorologija', name:'meteorologija' },
    { path:'/meteorolosko-bdenje', name:'meteorolosko bdenje' },
    { path:'/aktuelni-podaci', name:'aktuelni podaci' },
    { path:'/podaci', name:'Podaci' },
]

Upon reviewing your code again, I noticed that there should not be a leading slash in your children paths. Therefore, you need to update it as follows:

{ path:'/meteorologija', name:'meteorologija', component:()=>import('../pages/BlogPage'), children:[
        { 
            path:'meteorolosko-bdenje', name:'meteorolosko bdenje', component:()=>import('../pages/BlogPage'), 
            children:[
                { path:'aktuelni-podaci', name:'aktuelni podaci', component:()=>import('../pages/BlogPage'),
                    children:[
                        { path:'podaci', name:'Podaci', component:()=>import('../pages/BlogPage') },
                        { path:'trenutne-temerature', name:'Trenutne temperature', component:()=>import('../pages/BlogPage') },  
                        { path:'ekstremne-temperature', name:'Ekstremne Temperature', component:()=>import('../pages/BlogPage') },  
                        { path:'pritisak', name:'Pritisak', component:()=>import('../pages/BlogPage') },  
                        { path:'vjetar', name:'Vjetar', component:()=>import('../pages/BlogPage') },  
                        { path:'kolicina-padavina', name:'Kolicina padavina', component:()=>import('../pages/BlogPage') },  
                        { path:'radarska-slika', name:'Radarska Slika', component:()=>import('../pages/BlogPage') },  


                    ] 
                },

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

How can I use an HTML button to activate a function that inserts text into a read-only text-box?

Trying to write a simple piece of HTML code that finds the number greater than or equal to the first initial amount that wholly divides the second given amount. The code attempts to divide the numbers, and if it fails, increments the first number by 1 and ...

Issue with displaying value after rendering

After fetching pool data from the constants file, my task was to create a featured vault - the one with the highest APY Reward value obtained from the API. Even though I successfully fetched all three values and performed the calculations, I am facing an i ...

How can we customize HTML images without allowing third-party JavaScript to enlarge them?

I am using Blogger to host my website and I have a basic knowledge of HTML and CSS. I want to incorporate a collaborative add-your-link feature using SimplyLinked. However... They provided me with the following HTML: <script type="text/javascript" src ...

Changing in height by utilizing javascript's style.height animation

When attempting to adjust the height property using JavaScript instead of jQuery, a challenge arises. The issue lies in the inability to reset the height back to zero after setting it to the scrollHeight of the element. The following is the JavaScript cod ...

Recreating elements in ng-repeat using ng-click conditionally

I am attempting to swap an anchor tag with an image when clicked by the user. The code snippet I'm using looks like this: <div ng-repeat="postpart in currentPost.parts "> <div ng-if = "!postpart.isclicked"> <img ng-src= ...

Does Mongoose use asynchronous saving?

Just starting out with mongoose and node. I'm trying to determine if the mongoose document.save method is asynchronous. Based on my observations, it seems to work even when not connected. Is there any way to know for sure when the document has been s ...

A beginner's guide to integrating three.js into your React projects

I am having trouble integrating three.js into my React application. Despite following the basic sample from the three.js documentation, I am unable to load it on the canvas. Initially, I successfully implemented the sample in a vanilla.js sandbox. However ...

Can a robust web application be developed using Kotlin in Node.js?

With the recent release of Kotlin 1.1, it is now possible to compile Kotlin projects into JavaScript as a target, allowing for full JavaScript compilation. Can a Node.js application, like an express webserver, be developed entirely using Kotlin code? As s ...

Unable to import a text file using a semicolon as delimiter in d3

I'm just starting to learn JavaScript and the D3 library. I recently explored the "d3-fetch" module (available at https://github.com/d3/d3-fetch) and the d3.dsv command API (find it here: https://github.com/d3/d3-dsv). However, I am struggling to unde ...

Clickable Slider Picture

I'm having a minor issue with a jQuery script that slides images on my website. The script itself is functioning properly, but I am trying to add links to each image. I've attempted placing <a href> and </a> tags around the <img> ...

Script execution in '<URL>' has been prevented due to sandboxing of the document's frame and the absence of the 'allow-scripts' permission setting

When I deploy my pure Angular application with a REST API to a production server and try to access its URL from another site (such as a link in an email), I encounter a strange problem. Firefox doesn't provide any error message, but Chrome says: Blo ...

Can anyone explain how to conduct a live search using Ajax?

Hello, I'm currently working on a project where I've successfully connected a large database to a website and implemented an AJAX live search feature. However, I am now in need of a non-live version of the AJAX search. The current setup displays ...

Discovering the proper way to infer type for tss-react withParams and create

Greetings to everyone and a huge thank you in advance for your generous help. I have a desire to utilize the tss-react library for styling, particularly because I will be implementing Material UI components. As I delve into some documentation, the latest A ...

The issue of not being able to add data to the client is encountered in a local setup involving Socket.io, MSSQL

I have a large dataset that needs to be retrieved from a SQL server using Node.js and then broadcasted to clients in real-time using Socket.IO. I've been successful in fetching the data, but the UI on the client side isn't updating. Although I c ...

Unnecessarily intricate: A Comparison and Enumeration of Elements in Arrays

I am facing a challenge with organizing arrays that represent categories and subjects. Each array represents a category, while each item within the array is a subject. For example: 4 Categories with Subjects ['A','B','D'] [&a ...

Tips for submitting a checkbox value even when it is disabled

I attempted to make the checkbox readonly, but users were still able to check/uncheck the field. Next, I tried disabling the checkbox which successfully prevented user interaction. However, when attempting to submit the form, the disabled checkbox value ...

Using ajax.actionlink to display the desired text

When using an ajax.actionlink @Ajax.ActionLink("Add Last Name", // <-- Text to display "AddTimeSeriesData", // <-- Action Method Name etc.... @id = "link") How can I extract the link text in JavaScript? I attempted to use $(&apo ...

Creating a Custom Error Page in SpringBoot

After developing an application with SpringBoot that features webservices and a front-office coded in ReactJs, the default port was set to 8080. To simplify the URL access, I decided to switch this application to port 80 by adding the code snippet below to ...

Incorporating Common Types for Multiple Uses

Is there a way to efficiently store and reuse typings for multiple React components that share the same props? Consider the following: before: import * as React from 'react'; interface AnotherButtonProps { disabled?: boolean; onClick: (ev ...

How can the name of an element be passed as an argument to a function called by that element within a Vue component?

I'm currently developing an interactive SVG map inspired by the one found on the CDC page. In my SVG map component, I have implemented multiple path elements that each contain a unique name attribute representing the corresponding state. Additionally, ...