Is it possible to use multiple routes in the same page with Vue-router?

In the process of developing a Vue-based web application that utilizes vue-router in history mode, everything was functioning smoothly for navigating between various pages. However, a new request has been made to open certain pages within a virtual dialogue, which is causing some complications. Initially, we attempted using an iframe for this purpose, but it resulted in a loading impact.

It's important to note that the 'virtual dialogue' referred to is essentially a styled div meant to appear above the rest of the content like a window, capable of displaying other pages within the Vue app. It should not be mistaken for a genuine browser level dialogue box.

The structure of our site is as follows:

  • components/ContentDialogue.vue
  • layout/MainLayout.vue
  • pages/
    • MyPage.vue
    • MyPage2.vue
    • MyPage3.vue
  • router/index.js <-- router setup here
  • App.vue
  • main.js

MainLayout contains a <router-view/>, allowing the appropriate content to be displayed when entering a specific path.

The introduction of the dialogue poses challenges, as it needs to have the ability to display any of the other pages within the frame. This leads to the suggestion that MainLayout.vue should be structured as follows:

<template>
  <div class="layout main-layout">
    <div class="page-container">
      <router-view />
    </div>
    <div v-if="showDialogue" class="dialogue-page-container">
      <router-view />
    </div>
  </div>
</template>

The Vue router is configured in the router/index.js and integrated into the main file as shown below:

const app = {
  router,
  render: h => h(App)
};

const vue = new Vue(app);
vue.$mount('#app');

Although the concept appears sound, I'm uncertain about how to implement it practically. For the dialogue feature, options include triggering its opening through an event passed to MainLayout or incorporating it in a query value such as /mypage?popup=/mypage2. The challenge lies in translating this to the router and layout structures effectively.

If anyone has suggestions on how we can successfully execute this, your input would be greatly appreciated.

Answer №1

When setting up a layout with multiple views, it's more efficient to display them simultaneously rather than nesting them. Named views are useful in this scenario as they allow you to define and assign names to different outlets within your view. By doing so, you can have multiple outlets instead of just one, providing better organization for your content.

<router-view class="view left-sidebar" name="LeftSidebar"></router-view>
<router-view class="view main-content"></router-view>
<router-view class="view right-sidebar" name="RightSidebar"></router-view>

To render these views, components need to be used, meaning each view requires its own component even if they belong to the same route. This can be achieved by specifying components (plural) in the routes configuration:

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      components: {
        default: Home,
        // equivalent to LeftSidebar: LeftSidebar
        LeftSidebar,
        // matching the `name` attribute on `<router-view>`
        RightSidebar,
      },
    },
  ],
})

For more detailed information, visit:

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

What is the best way to establish a connection between the same port on Expressjs and Socket.io

I am currently using Express.js and Socket.io to develop a chat application. Initially, I created my project with Express-Generator and began by running the node ./bin/www script. However, I decided to remove the ./bin/www file and instead combined it wit ...

What is the best way to delay a method in vue.js?

In the tab element called competences, there is an input field with the reference search. <ul class="nav nav-tabs" id="myTab" role="tablist"> <li class="nav-item"> <a @click="competencesTabClick" aria-controls="Company" aria-sel ...

Encountering 404 Error in Production with NextJS Dynamic Routes

I'm currently working on a next.js project that includes a dynamic routes page. Rather than fetching projects, I simply import data from a local JSON file. While this setup works well during development, I encounter a 404 error for non-existent pages ...

Tips for executing an npm command within a C# class library

I am currently developing a project in a class library. The main objective of this project is to execute a JavaScript project using an npm command through a method call in C#. The npm command to run the JavaScript project is: npm start The JavaScript ...

Would you be able to clarify why the run() function is giving me an error when I try to return {1,3}, but it works fine when I return {a,b} in

I'm really confused as to why the return {1,3} in the run() function is throwing an error when it works just fine for return {a,b} in the fun() function function fun(){ let a = 10; let b = 20; return {a, b}; } ...

Preventing variables from reinitializing in express.js

Within my app.js file, I invoke a search function that communicates with an LDAP server. Upon doing so, the server sends back a cookie which is essential for my subsequent queries. My intention was to save this cookie as an app.local variable in my app.j ...

Conceal any elements designated by a particular class and reveal them based on their corresponding ID

I have utilized jQuery's hide() function to initially hide all elements of a specific class when the page loads. My goal is to make individual elements visible again based on their unique IDs when a corresponding link is clicked. In total, there are ...

instructions for creating a hover effect where one div vanishes when hovering over another div

Is there a way to make the line visible when hovering over my circular div? #line { display: none } <div id='circle'> <div id= 'line'> ...

Using promises and the fetch function to connect to a database

I am attempting to utilize promises with the fetch() function in order to access MongoDB from the front-end, but I am encountering some issues. var Promise = () => ( new Promise((resolve, reject) => { //perform certain actions, make requests ...

Populate a dropdown list with array elements using Javascript in ReactJS

I am encountering an issue while trying to populate a dropdown with data from the 'options' array. The error message states that it cannot read property appendChild of null. Using 'document.getElementByClassName' instead of document.ge ...

Guide on modifying cube material dynamically in WebGL at runtime

Currently, I am utilizing three.js to create animations. My goal is to dynamically modify the material of a cube mesh. Below is an example: // Create cube geometry var material1 = [new THREE.MeshBasicMaterial({color:0xBEE2FF}),.....]; var geometry = new ...

Changing the model does not automatically update the visibility of ng-show

I have a fragment of my view that simply displays a loading indicator. Html: <span class="app-loading-container"> <span class="app-loading-animation" ng-show="loading"></span> </span> When I initially load the page, the span ...

disable the react .hover behavior once clicked

Can someone help me figure out why my attempt to cancel the .hover on function using .off is not working? $("document").ready(function(){ $("button").hover(function(){ $("h1").hide(); }, function(){ ...

What is the best way to switch between components in vue.js?

I have created a Dashboard.vue component consisting of two child components: DisplayBooks.vue and sortBooksLowtoHigh.vue. Initially, the sortBooksLowToHigh component is hidden while the displayBooks component is visible by default. The requirement is that ...

An error occurred at line 120 in the index.js file of the http-proxy library: "socket hang up"

I encountered an issue while running expressJS in one of the containers within docker-compose. When I repeatedly refresh the landing page by pressing CMD+R (approximately every 3-4 seconds), it displays an error message "Error: socket hang up" causing the ...

The robots.txt file in Nuxt.js allows for multiple disallow directives for each user agent

With the Nuxt module called nuxt-robots, how can I set up multiple disallow rules per user agent? Currently, my configuration looks like this: robots: () => { return { UserAgent: '*', Disallow: '/search/', Si ...

Using Angular JS to filter ng-repeat with a combination of a field and a dropdown

There seems to be a lot of conflicting information online regarding this issue, but I am in search of a concise solution. My dataset consists of a list of countries around the world, including their name, ISO alpha code, region, and more. To display this ...

Guide on deactivating the div in angular using ngClass based on a boolean value

displayData = [ { status: 'CLOSED', ack: false }, { status: 'ESCALATED', ack: false }, { status: 'ACK', ack: false }, { status: 'ACK', ack: true }, { status: 'NEW', ack ...

Inconsistencies observed during the native JSON import process in JavaScript

Encountered a strange behavior when loading a JSON file into JavaScript while working on a React project. Seeking an explanation and guidance on properly accessing data from the JSON data store. The JSON file contains product data: { "product ...

Transform the JSON response into a map

I have a functioning code that I am looking to adjust. let table_specs = {'columns': [ {'name': 'Col1', 'width': 7, ....}, {'name': 'Col2', 'width': 8, . ...