简体   繁体   English

如何创建与 vue 路由器一起使用的子组件?

[英]How can I create a subcomponent to use with vue router?

First, I'm new to vue.js , so I apologize if my question is a bit basic.首先,我是vue.js ,所以如果我的问题有点基础,我深表歉意。 My App.vue is:我的App.vue是:

<template>
  <div id="app">
    <router-view />
  </div>
</template>

I have a router.js , which has:我有一个router.js ,它有:

import Vue from 'vue';
import Router from 'vue-router';

import Container from "./components/Container.vue";
import Home from "./components/Home.vue";
import Login from "./components/Login.vue";

Vue.use(Router);

export default new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [{
            path: '/',
            name: 'login',
            component: Login,
        },
        {
            path: '/home',
            name: 'home',
            component: Container,

        },
    ],
});

The idea is that Container has navigation, and should be used for various components (including Home ).这个想法是Container有导航,应该用于各种组件(包括Home )。 How can I do that?我怎样才能做到这一点?

Just like you have a App.vue containing router-view , you need a Container component to contain another router-view component.就像你有一个包含router-viewApp.vue一样,你需要一个Container组件来包含另一个router-view组件。 This will create a nested router views.这将创建一个嵌套的路由器视图。

Once you do that, you will need to add children routes:完成此操作后,您将需要添加子路由:

export default new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [{
            path: '/',
            name: 'login',
            component: Login,
        },
        {
            path: '/home',
            component: Container,
            children: [
                {
                    // LEAVE THIS PATH BLANK. Matches /home
                    path: '',
                    name: 'home',
                    component: HomeComponent,
                },
                {
                    path: 'another-route',
                    name: 'nested-route',
                    component: AnotherComponent,
                }
            ]
        },
    ],
});

The key is to add children routes and leave one of the children with a blank path.关键是添加子路径,并为其中一个子路径留下空白路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM