Vue.js 如何使用多个路由视图,其中一个在另一个组件中?

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

我有一个 Vue.js 单页应用程序,其中有一个使用 <router-view/> 呈现不同页面的主导航栏。

像这样:

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

在其中一个页面中,我有一个侧边栏,其中包含更多导航链接(使用 <router-link/> 就像主导航栏一样。

像这样:

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

当我单击边栏导航链接时,我希望边栏旁边的内容发生变化,url 也发生变化。但是,我丢失了侧边栏,只获取了要在内容部分呈现的组件。

如何达到预期的效果?如何使用多个 <router-view/>,其中一个在另一个组件中,如上例所示?

您需要使用named views。为视图提供 name 属性。

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

并像

那样配置它们
const router = new VueRouter({
  routes: [
    {
      path: '/',
      components: {
        default: Foo,
        a: Bar
      }
    }
  ]
})

请参考official docs

侧边栏消失的原因是除了 <main-header>.

之外,所有组件都在第一个 <router-view> 中呈现

您应该通过在边栏路由器中配置 children 来使用嵌套路由器,例如:

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
        }
      ]
    }
  ]
})

更多信息见nested routes

@adoug 的回答对我有帮助。

但就我而言,我将两个路由器视图都命名为:

我这样做是为了修复它:

<router-view name='a'/>
<router-view name='b'/>

你有 ,在你体内的某处 FatherComponent.vue 安装在 a 你有第二个

我这样做是为了修复它:

const router = new VueRouter({
    routes: [
        { path: '/your-sidebar-url',
            components: {
                a: FatherComponent //you sidebar main component in 'a' name of routed-view
            },
            children: [
                {
                    // A will be rendered in the second <router-view>
                    // when /your-sidebar-url/a is matched
                    path: '/child-path/a',
                    components: {
                        b: ChildComponentA //note that 'b' is the name of child router view
                    }
                },
                {
                    // B will be rendered in the second <router-view>
                    // when /your-sidebar-url/b is matched
                    path: '/child-path/b',
                    components: {
                        b: ChildComponentB //note that 'b' is the name of child router view
                    }
                }
            ]
        }
    ]
})

命名路线视图

const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }
const Baz = { template: '<div>baz</div>' }

const router = new VueRouter({
  mode: 'history',
  routes: [
    { path: '/',
      // a single route can define multiple named components
      // which will be rendered into <router-view>s with corresponding names.
      components: {
        default: Foo,
        a: Bar,
        b: Baz
      }
    },
    {
      path: '/other',
      components: {
        default: Baz,
        a: Bar,
        b: Foo
      }
    }
  ]
})

new Vue({
    router,
  el: '#app'
})
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>

<div id="app">
  <h1>Named Views</h1>
  <ul>
    <li>
      <router-link to="/">/</router-link>
    </li>
    <li>
      <router-link to="/other">/other</router-link>
    </li>
  </ul>
  <router-view class="view one"></router-view>
  <router-view class="view two" name="a"></router-view>
  <router-view class="view three" name="b"></router-view>
</div>
https://router.vuejs.org/guide/essentials/named-views.html#nested-named-views