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 process for obtaining a compilation of JavaScript files that are run when an event is triggered?

How can I generate a list of all the JavaScript files that are triggered when a button is clicked or an input field is selected in a complex system? Is it possible to achieve this with Chrome DevTools, or are there alternative solutions available? If Chro ...

Can I modify a property in DataTables.Net using the data itself?

I am trying to set the "column" property based on the ajax data that I receive. The json data contains a "data" and "columns" property, so in order to extract the data, my code would look something like this: primaryTable = $('#example').DataTa ...

Learn the step-by-step process of dynamically adding elements to a JavaScript object in JSON structure

We are attempting to dynamically generate a JSON object using a for loop. The intended result should resemble the following: posJSON = [ { "position": [msg[0].Longitude, msg[0].Latitude], "radius": 0.05, "color": [255, 255, 0, ...

Update the style class of an <img> element using AJAX

My success with AJAX enables PHP execution upon image click. However, I seek a real-time visual representation without page reload. Thus, I aim to alter <img> tag classes on click. Presently, my image tag resembles something like <img title="< ...

The function is unable to accurately retrieve the state value

In my app, I have a component where I'm attempting to incorporate infinite scroll functionality based on a tutorial found here. export const MainJobs = () => { const [items, setItems] = useState([]); const [ind, setInd] = useState(1); const ...

Developing a Highchart Graph using JavaScript

I am trying to develop a high chart graph. Check out my code on FIDDLE LINK. I have two values, a and b, for the x-axis and y-axis. The issue arises when the difference between a and b is minimal, such as when both are equal (-9). In such cases, the grap ...

What is the best way to transmit a JavaScript array to a servlet via AJAX?

Within a textarea, users input one or more email addresses separated by commas. Here is my JavaScript code: var emails = $("#emails").val().split(","); if (emails.length == 0) { window.alert("Please enter an email address."); ...

Adding key/value pairs to an array of objects in RxJS Observables can be easily

I currently have an Angular app with state management that retrieves data from a database in observables. Here is an example of the interfaces I am working with: interface Service { id: number, name: string, category_id: number, } interface Category ...

Controlling data tables with knockout.js

I have successfully integrated an API in knockout.js, but I am facing an issue with displaying the amount based on accounting principles. My table definition includes columns for id, name, debit, credit, and amount. Since not all amounts fall under debit o ...

developing a loading animation with progress indicator in CSS3

I have been working on creating a preloader, but I am having trouble embedding the percentage with the CSS circle. So far, I have tried various plugins without success. Can anyone help me with this issue? Here is my current progress. Below is the HTML co ...

Error: Unable to access 'push' property of null object in Next.js Link

Utilizing Vite to develop a reusable component has led to an error upon publishing and reusing it: TypeError: Cannot read properties of null (reading 'push') The code for the component is as follows: import React from "react"; import ...

Can one extract the content from a secure message received from a Telegram bot?

Currently, I am utilizing the sendMessage() function with protected_content: true in order to prevent Telegram users from forwarding my bot's messages to others. Prior to implementing this setting, the text below was easily copyable. However, after e ...

"Experiencing a problem with Next.js 13 where the User Context is not functioning properly outside of _app

When using Next.js 13 and the user context, I've noticed that it doesn't function properly outside of _app.tsx. Any idea why? src>context>UserPositionContext.tsx import { createContext, useContext, useState } from "react"; const ...

When the mouse button is released or when an event listener is

I've been pondering a question that has yet to be fully answered. When I implement this technique to catch a mouse up event: <div onmouseup="/*Script to be executed*/"></div> Is it more efficient than this newer approach: <div id=" ...

the function does not output any value

I am currently facing an issue with my function called IsValidUrl(). This function is supposed to return values based on a certain condition (either false or true). However, there seems to be another function within it that is preventing the values from be ...

Rendering a component and updating state with inline onClick event handlers

When discussing the concept of pure render methods in React and highlighting the serious anti-pattern of setting state inside the render function, how strictly should this be adhered to? It is understood that triggering a setState within the render functio ...

Guide on Applying a Dynamic Color in VueJs 3 Composition API/Vuetify Using CSS

Currently, my project utilizes Vue 3 with the composition API and Vuetify for the UI. I am looking to utilize a color that is already defined in a Vuetify theme variable within my CSS, similar to how I have done it previously in JavaScript. Although I at ...

Idle Time in Nextjs - Making the Most of D

I've been experiencing a significant delay of around 6 seconds when refreshing my Next.js platform. As part of my debugging process to identify the root cause of this issue, I uncovered that approximately 5 seconds of this time is classified as idle. ...

Exploring the benefits of integrating ES6 modules in Express server technology

Is it possible to utilize ES6 modules in my Express application without relying on babel or @std/esm? find an alternative method that doesn't involve transpiling or using esm? Once I have started working on app.js in Express, it seems challenging to ...

Receiving multiple NodeJS Responses through AJAX for a single request

I have been working on a WebApp that involves heavy AJAX calls from the frontend and NodeJS Express at the backend. Here is a glimpse of my Frontend Code- Below is the global AJAX function I consistently use in all my projects: function _ajax(params = {}, ...