Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Vue.js how to use multiple router-views one of which is inside another component?

I have a Vue.js single page application where there is a main navbar that uses <router-view/> to render different pages.

Something like this:

<main-header/> <!-- navigation links -->
<transition name="slide-fade" mode="out-in">
  <router-view/> <!-- different pages -->
</transition>

In one of those pages I have a sidebar that has more navigation links (that are using <router-link/> just like the main navbar.

Something like this:

<sidebar/> <!-- navigation links -->
<div class="content">
  <transition name="slide-fade" mode="out-in">
    <router-view/> <!-- content beside the sidebar -->
  </transition>
</div>

When I click on the sidebar navigation links I want the content beside the sidebar to change as well as the url to change. However, I lose the sidebar, and just get the component that is to be rendered in the content section.

How do I achieve the desired result? How do I use multiple <router-view/>s one of which is inside another component, like the example above?

like image 617
wrahim Avatar asked Jan 05 '18 10:01

wrahim


People also ask

Can I use multiple router-view?

Instead of having one single outlet in your view, you can have multiple and give each of them a name. A router-view without a name will be given default as its name.

What is nested routing in VUE JS?

Note that nested paths that start with / will be treated as a root path. This allows you to leverage the component nesting without having to use a nested URL. As you can see the children option is just another Array of routes like routes itself. Therefore, you can keep nesting views as much as you need.

How to display the mounted Router component in vue router?

However, Vue Router has no way to display the mounted Home.vue component. To do this, you need to add the router-view component inside your App.vue file. This component tells Vue Router to mount any component associated with a route where <router-view /> is. Save and exit the main.js file. Next, open the App.vue file.

How to work with single page applications with vue router?

When working with single-page applications, there are a few caveats you need to be aware off. Since every page is bootstrapped into a single HTML page, navigating between internal routes using the standard anchor ( <a />) will not work. Instead, you will need to use the <router-link /> component provided by Vue Router.

How do I use multiple <router-view/> s one inside another component?

How do I use multiple <router-view/> s one of which is inside another component, like the example above? You need to use named views. Provide the name attribute to the view. const router = new VueRouter ( { routes: [ { path: '/', components: { default: Foo, a: Bar } } ] }) Please refer to the official docs.

How to use router-view and router-link in a Vue template?

Using <router-view> and <router-link> There are two directives our Vue application gives us to use in our template: <router-view /> - When a route is navigated to in the browser, this is where the component is rendered. For example, in our code going to / will render the Home component where we list <router-view />.


2 Answers

You need to use named views. Provide the name attribute to the view.

<router-view class="view one"></router-view>
<router-view class="view two" name="a"></router-view>

And configure them like

const router = new VueRouter({
  routes: [
    {
      path: '/',
      components: {
        default: Foo,
        a: Bar
      }
    }
  ]
})

Please refer to the official docs.

like image 89
Tuan Pham Avatar answered Sep 19 '22 11:09

Tuan Pham


The reason the sidebar disappeared is all the components are rendered in the first <router-view> besides the <main-header>.

You should use the nested router by configuring children in the sidebar router like:

const router = new VueRouter({
  routes: [
    { path: '/your-sidebar-url', component: your-sidebar-page,
      children: [
        {
          // A will be rendered in the second <router-view>
          // when /your-sidebar-url/a is matched
          path: 'a',
          component: A
        },
        {
          // B will be rendered in the second <router-view>
          // when /your-sidebar-url/b is matched
          path: 'b',
          component: B
        }
      ]
    }
  ]
})

More info in nested routes

like image 41
adoug Avatar answered Sep 20 '22 11:09

adoug