Vue components fail to display properly when transitioning between routes in Vue (version 3) using the Vue Router

Just diving into VueJS version 3 and vue-router. Despite my efforts to troubleshoot by consulting Stack Overflow and Google, the issue remains unresolved.

I have defined two routes:

  1. /login
  2. /admin/index

The Problem:

While the login route functions correctly, attempting to access the admin route leads to displaying the login form instead of the admin panel.

NOTE : I have placed

<router-view></router-view>
within
components/layouts/MasterComponent.vue
. In App.vue, only the login component is imported.

Here is a snippet of the code:

routes:

import { createRouter, createWebHistory } from 'vue-router'
import LoginComponent from "../components/auth/LoginComponent";
import MasterComponent from "../components/layouts/MasterComponent"; 
// Remaining route definitions ...
export default router;

Master component code:

// Code for MasterComponent.vue file ...
export default {
    name: "MasterComponent",
    components: {
        // Components imported here
    },
};

App.vue code

// Code for App.vue file ...
export default {
    name: 'App',
    components: {
        // Login Component
    }
}

main.js code

import { createApp } from 'vue'
import App from './App.vue'
import routes from "./routes/route";
// Bootstrap and CSS imports ...
const app = createApp(App)
app.use(routes)
app.mount('#app')

Answer №1

ISSUE RESOLVED

I realized that I had forgotten to include

<router-view></router-view>
in my App.vue file. Additionally, I needed to update my routes as shown below:

// updated admin routes
    {
        name: "Index",
        path: "/admin",
        component: MasterComponent,
        redirect: '/dashboard',
        children: [{
            path: '/dashboard',
            name: 'Dashboard',
            component: DashboardComponent,
        }]
    },

App.vue before modifications:

<template>
  <LoginComponent/>
</template>

<script>
import LoginComponent from './components/auth/LoginComponent.vue'

export default {
  name: 'App',
  components: {
    LoginComponent
}
}
</script>

<style>
</style>

App.vue after updates:

<template>
  <router-view></router-view>
</template>

<script>

export default {
  name: 'App',
  components: {
}
}
</script>

<style>
</style>

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

JavaScript tool: Verify the presence of both jQuery and jQuery UI

I have been working on developing a Javascript Widget (almost complete), and when integrating it into the first site I encountered some issues. My Widget requires loading multiple libraries (jquery, jquery ui, mustache, ...); this is achieved by injecting ...

The placement of the React.js/Next.js Loader is incorrect on the page

While I was trying to display a Loader over everything during data fetching from my API, I encountered a situation where the Loader was not appearing at the expected top level but inside the page itself. Even though the HTML tree showed it at the top level ...

Implement an onClick event to the newly created th element using document.createElement()

Through the use of document.createElement("th"), I am dynamically inserting columns into a table. var newTH = document.createElement('th'); Is there a way to add an onClick attribute to this element so that users can delete a column by clicking ...

Retrieving Information from an API with Custom Headers in React Native

I have an API that necessitates specific headers for access. Without these headers, a browser displays the following error: Code: 4101 Message: Header X-Candy-Platform is required However, when the headers are provided, the API returns a json response. ...

`Struggling with managing callbacks, handling errors, and working with MongoDB`

I recently revamped my code for an application that handles adding companies to a database. In the past, my code was messy and unorganized, so I decided to structure it properly by introducing routes, a controller, and a data access object. Here is how my ...

Implementing ng-if with asynchronous functions: A step-by-step guide

The objective here is to display an image in a template only if the ratio of its dimensions is greater than 2. <img class="main-img" ng-if="showImage($index)" ng-src="{{item.img}}"> Implementation: $scope.showImage = function(index) { var img ...

javascript download multiple PDF files on Internet Explorer

I am facing an issue with downloading multiple PDF files In my list, I have various a elements with links to different PDF files. I created a loop to go through each a element and generate an iframe using the value of the href as the source. This solutio ...

Setting up a chat feature in a Laravel application: A step-by-step guide

Looking to Enhance My Web-Application I have been working on a web-application that utilizes: Laravel for the back-end Angular for the front-end. Milestone Achieved! I successfully implemented the entire user authentication process including: User Aut ...

When using vue-resource for an http request, the error message "_.isArray is not a function" may be

Attempting to retrieve an object from a server located at localhost:3000. The object is visible when accessing the address via a web browser. In my Vue instance's methods property, I have a function that is executed on the front end: goToTutors: fun ...

React's .map is not compatible with arrays of objects

I want to retrieve all products from my API. Here is the code snippet for fetching products from the API. The following code snippet is functioning properly: const heh = () => { products.map((p) => console.log(p.id)) } The issue ari ...

Encountering the error "TypeError: Unable to access property 'findAll' of undefined" when using Sequlize

Currently, I am in the process of developing a CRUD Application utilizing PostgreSQL, Sequelize, and Express. I have been referring to this specific tutorial as my guide. However, it appears that there might be an issue with either my model or database con ...

Create a class for the grandparent element

Is there a way to dynamically add a class to a dropdown menu item when a specific child element is clicked? Here's the HTML structure I have: <ul id="FirstLevel"> <li><a href="#">FirstLevel</a></li> <li>< ...

Using Pinia to save initial server API state in Nuxt3: A step-by-step guide

Initially, I am looking to store data in Pinia when the site loads so that I can access the data from Pinia while navigating through pages without having to continuously call the API. However, when attempting to utilize the data with array methods like fi ...

Could you show me how the easyrtcid is generated in the demonstration of audio-only chat? I would appreciate a step-by

Currently, I am utilizing the easyrtc webpage to test out the audio only chat demo and everything seems to be running smoothly. However, when connecting, the easyrtcid variable is automatically assigned a random string. I was wondering if there is a way t ...

What is the reason for the continual influx of new users being added to the database?

I have a Node.JS and MongoDB console application where I've implemented adding users in one file and outputting all collection objects to the console in another file. When running the command - node scripts/get_all_users.js, both existing users are di ...

Send parameters to the v-dialog component instead of the routing mechanism

I've been working on my vue application and I'm looking to make a change. Instead of opening a new tab, I want to use a v-dialog to address the issue. Currently, I am passing the id to the component like this: menuActions(option, project) { ...

Should a React application perform a complete refresh when a file is reloaded?

Recently, I delved into the world of React and learned about one of its key benefits: updating only the necessary DOM elements. However, as I began building an app from scratch, I encountered a situation where every time I saved the .js file, it resulted ...

Having trouble importing the module I developed in Node

I've been struggling to understand why this import is failing. Despite trying numerous approaches, modifying the export and import based on various tutorials online, it continues to result in failure every time. This should be a simple task in theory. ...

Attempting to modify PHP query following submission of data via Axios in Vue application

Fetching a JSON object through a PHP query from a service known as BullHorn can be done like this: <?php echo getBhQuery('search','JobOrder','isOpen:true','id,title,categories,dateAdded,externalCategoryID,employmentTy ...

How does the call method on array.prototype.includes work with arguments x and y?

Curious about the functionality of array.prototype.includes.call(x, y);. Discovered that includes() confirms if an array has the specified value and provides a true or false result. Learned that call() invokes this alongside any optional arguments. The ...