Rendering nested routes in Vue router with the parent component

I attempted to nest my routes using Vue router, but encountered some difficulties

{
    path: '/admin',
    name: 'Admin', component: () => import('pages/Admin'),
    children:[
      { path: 'stock', name: 'Stock', component:()=> import('pages/Stock')},
    ]},

After some troubleshooting, I discovered that I needed to place <router-view> inside the parent component for it to work properly.

However, when I try to load the page /admin/stock, both components are rendered on top of each other. Why does the parent component (/admin page) still display?

It's worth noting that without nesting the routes, everything worked perfectly fine and the components rendered separately (as shown below).

{
    path: '/admin',
    name: 'Admin', component: () => import('pages/Admin'),
    children:[
     //no nested route
    ]},
  { path: 'admin/stock', name: 'Stock', component: ()=> import('../pages/Stock')},

Thank you for your assistance

Answer №1

For the "Admin" component, it is recommended to include a router-view tag. This way, the Admin component can serve as a layout and render the appropriate children based on the current route.

Here is an example:

Admin Component:

<template>
    <div>
        <div>
            Content that appears in all child components
        </div>
        <router-view>
            <div>"Admin" page content</div>
        </router-view>
    </div>
</template>

Stock component:

<template>
    <div>
        "Stock" content
    </div>
</template>

When navigating to /admin, the following will be rendered:

<div>
    <div>
        Content that appears in all child components
    </div>
    <div>"Admin" page content</div>
</div>

And when visiting /admin/stock, this will be rendered:

<div>
    <div>
        Content that appears in all child components
    </div>
    <div>"Stock" content</div>
</div>

For a more detailed example, refer to this link.

If you do not need to reuse the "Admin" component layout, you have the option to define routes without nesting them as shown in the second case mentioned.

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

Inconsistent behavior between Chrome and Firefox when using AngularJS $resource GET method: success in Chrome but error

I am currently working on a simple custom GET request in Angular using $resource angular.module('myApp') .factory('MyService', function($resource){ return $resrouce('some url', {}, { list: {method:'G ...

Transitioning from using a jQuery get request to utilizing Vue.js

Looking to transition from JQuery-based js to Vue as a newcomer to JavaScript. Seeking guidance on making a get request and displaying the retrieved data. What approach would you recommend for this task? Here's the HTML snippet: <div> <di ...

jQuery for validating input fields with positive integers only using regular expressions

I currently have two input fields in my HTML that look like this: <input type="text" class="txtminFeedback" pattern="^\d+([\.\,][0]{2})?$" placeholder="Minimum Feedback"> <input type="text" class="txtmaxFeedback" pattern="^\d ...

JavaScript: unable to locate information within JSON reply

Looking to develop a Twitter bot utilizing the Twitter and Spotify APIs. Making progress, but encountered an issue I can't tackle alone: Seeking the artist and song name of the top 1 track from the top 50 Spotify songs. Upon sending a request to the ...

jQuery function for shuffling the order of a random div?

Upon loading the page, I have implemented a code that randomizes the order of the child divs. Here is the code snippet: function reorder() { var grp = $("#team-posts").children(); var cnt = grp.length; var temp, x; for (var i = 0; i < cnt; ...

Error: Unable to access the 'ht_4year_risk_opt' property because it is null

When attempting to call the servlet that returns JSON data, I encounter an error while parsing the data. var jsonResponse = jQuery.parseJSON(data); var ht_4year_risk_opt = jsonResponse.ht_4year_risk_opt; ...

Locate a specific item in an array using AngularJs based on a key and present its value on the View

Imagine you have an array of objects: $scope.objArr = [{key:1,value:'value1'},{key:2,value:'value2'},{key:3,value:'value3'}]; And a variable that corresponds to key. For instance: $scope.a = 3; In the Controller, you want ...

Passing parameters to a Vue.js @click method in Laravel 6: A step-by-step guide

How can I pass a parameter to a Vue component? Any help is appreciated! Blade Template @extends("./layouts.app") @section("title") {{$shop->shop_name}} @endsection @section("content") <addToCart></addToCart> @endsection Vue Js Code & ...

What is the process for displaying a hidden div using a button?

I'm running into a problem with my project. Here is the code I've been using to hide my div element : @keyframes hideAnimation { to { visibility: hidden; } } To trigger the animation and hide the div after 6 seconds, I have implemented ...

What could be the reason behind jQuery replacing the entire span with .text?

I am dealing with the following HTML code snippet: <p><input id="revenue" type="text" value="100000" /><span id="howmuch"><a href="" class="ka_button small_button small_cherry" target="_self" style="opacity: 1; "><span>How Mu ...

Initial button click fails to trigger onclick event

After researching and reading various articles on the issue, I have tried several solutions but nothing seems to work. The problem occurs when the button is clicked for the first time - nothing happens. It is only after clicking the button a second time th ...

Encountering issues when adding information to a React table

Every time I try to submit data, I encounter an error message. Line 53: Expected an assignment or function call and instead saw an expression no-unused-expressions I am attempting to add user-submitted data onto a table as td elements. Could someon ...

The clearing of sessions is not supported by Node.js / Express.js

At the start, the application loads some users using Angularjs / $http.get(). As you scroll down, more users are loaded dynamically (infinite scroll). In addition to this, there are location filters on the page. Users can select a different Country and Ci ...

There was a TypeError encountered in a Node application, stating: "Unable to access the 'trim' property of an undefined

Currently, I am developing a JavaScript application that follows the MVC model. In the controllers, there is a userController.js file, and in the models, there is a user.js file which contains a cleanup function. However, I am encountering a TypeError: Can ...

Retrieving various pieces of data in Express

After scouring through various websites, I am still unclear on how to extract multiple GET variables using Express. My goal is to send a request to a Node.JS Express server by pinging the URL with: file_get_contents('http://127.0.0.1:5012/variable1/v ...

Unleashing the full potential of ajax through endless scrolling performance

Recently, I implemented an infinite scroll feature on my website's index page by making ajax requests to retrieve a JSON containing 40 objects. Then, I add them using a JavaScript loop. However, I've noticed that it slows down the site at times. ...

Is there a way to tally up the overall count of digits in a number using TypeScript?

Creating a number variable named temp in TypeScript: temp: number = 0.123; Is there a way to determine the total count of digits in a number (in this case, it's 3)? ...

Combining two arrays by finding common elements

Currently, I am working on a project where I retrieve data from the server, and each piece of data has to adhere to a specific format: const DATA = [ { title: {title: 'Main dishes'}, data: [ {_id: 1, type: 'Pizza'}, ...

Javascript-generated HTML elements are invisible

I am attempting to create a "circle of fifths" using html, css, and javascript. I am following this tutorial: https://blog.logrocket.com/interactive-svg-circle-of-fifths/ Although I am using the astro framework, I don't believe my issue is related to ...

Is there a way to invoke a function using $scope within HTML that has been inserted by a directive in AngularJS?

After moving the following HTML from a controller to a directive, I encountered an issue where the save button no longer functions as expected. I attempted to change the ng-click to a different function within the directive code, and while that worked, I u ...