Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue Router multiple independent router-view navigation

I have multiple blocks of items similar to the books in the jsfiddle below. Each book has a sub-menu and users can click to see a specific book content.

The problem is when users visit book1/summary , all other books will also switch to Summary Component. I don't want that, I want all other books stay at where they are, independently . For example: book1 is showing Summary, book2 is showing Condition, book3 is showing Reservation and each one of them is independent from others.

Can somebody help ? Thanks a lot.

https://jsfiddle.net/trachanh/pL4rmua6/2/

<div id="app">
  <router-link to="/">/home</router-link>
  <router-link to="/foo">/foo</router-link>
  <router-link to="/books">/books</router-link>
  <router-view></router-view>
</div>

<template id="books">
 <div> 
  <div class="book" v-for="book in books">

  <h3>{{book.title}}</h3>
      <ul>
          <router-link :to="{ path: '/books/'+book.id+ '/summary'}">Summary</router-link>
          <router-link :to="{ path: '/books/'+book.id+ '/location'}">Location</router-link>
          <router-link :to="{ path: '/books/'+book.id+ '/condition'}">Condition</router-link>
          <router-link :to="{ path: '/books/'+book.id+ '/reservation'}">Reservation</router-link>
      </ul>    
      <router-view></router-view>
  </div>
 </div>
</template>


  const Home = { template: '<div>Home</div>' }
  const Foo = { template: '<div>Foo</div>' }

  const Books = {
    template: '#books',
    data: function(){
        return {
          books: [
            {id: 1, title: 'Harry Potter'},
            {id: 2, title: 'Twilight'},
            {id: 3, title: 'The Hobbit'}
          ]
        }
    }
  }

  const summary = { template: '<div>Summary component</div>' }
  const location = { template: '<div>Location component</div>' }
  const condition = { template: '<div>Condition component</div>' }
  const reservation = { template: '<div>Reservation component</div>' }

  const router = new VueRouter({
    mode: 'history',
    routes: [
      { path: '/', component: Home },
      { path: '/foo', component: Foo },
      { path: '/books', component: Books,
        children: [
                {
                  path: ':bookID/summary',
                  component: summary
                },
                {
                  path: ':bookID/location',
                  component: location
                },
                {
                  path: ':bookID/condition',
                  component: condition
                },
                {
                  path: ':bookID/reservation',
                  component: reservation
                }
          ]
    }]
  })

  new Vue({
    router,
    el: '#app',
    data: {
      msg: 'Hello World'
    }
  })
like image 655
Nguyen Anh Vu Avatar asked Mar 09 '17 21:03

Nguyen Anh Vu


People also ask

Can you have multiple vue routers?

Conclusion. We can have multiple router-view components with Vue Router 4. Also, we can name our routes so we can use that for navigation.

Can we define multiple router outlets in a component vue?

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. A working demo of this example can be found here.

Can you pass Props to router-view vue?

Vue Router 4 provides multiple ways of doing this: from setting the props property on the route record to true and automatically passing all params as props, setting the props property to a static object to provide static props, or setting it to a function that returns the desired props.


1 Answers

You should be able to make it working using CHILD ROUTES, but you would need to slightly change your setup.

Every book needs separate navigation inside and <router-view> inside it and then calling child route inside the particular book should work.

Try to refer to docs: https://router.vuejs.org/en/essentials/nested-routes.html

like image 64
Marek Urbanowicz Avatar answered Oct 01 '22 10:10

Marek Urbanowicz