Vue children路由器:url已更改但显示父组件

时间:2017-11-06 08:16:50

标签: javascript vue.js vue-router

我正在学习Vue并坚持使用嵌套路由器,我在路由中定义了一些子路由器,但是当我访问子路由时它仍然显示父组件,我的代码如下:

App.vue:

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <router-link :to="{name: 'Home'}">Home</router-link>
    <router-link to="/cart">Cart</router-link>
    <router-link to="/admin">Admin</router-link>
    <router-link to="/admin/add">【Admin Add】</router-link>
    <router-link to="/admin/edit">Admin Edit</router-link>

    <router-view/>
  </div>
</template>

<script>
export default {
  name: 'app'
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

路由器/ index.js:

import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/pages/Home'
import Cart from '@/components/pages/Cart'
import Index from '@/components/pages/Admin/Index'
import Add from '@/components/pages/Admin/Add'
import Edit from '@/components/pages/Edit'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home
    },
    {
      path: '/cart',
      name: 'Cart',
      component: Cart
    },
    // {
    //   path: '/admin/index',
    //   name: 'Index',
    //   component: Index
    // },
    // {
    //   path: '/admin/add',
    //   name: 'Add',
    //   component: Add
    // },
    // {
    //   path: '/admin/edit',
    //   name: 'Edit',
    //   component: Edit
    // }
    {
      path: '/admin',
      // name: 'Admin',
      component: Index,

      children: [
        {
          path: 'add',
          name: 'Add',
          component: Add
        },
        {
          path: 'edit',
          name: 'Edit',
          component: Edit
        }
      ]
    }
  ]
})

我尽量不使用子路由器,它会正确显示组件,就像被评论的代码一样。

我对此很困惑,请帮助我。

1 个答案:

答案 0 :(得分:5)

Index组件中,您需要添加<router-view></router-view>。从vue-router working example中查看此docs

const User = {
  template: `
    <div class="user">
      <h2>User {{ $route.params.id }}</h2>
      <router-view></router-view>
    </div>
  `
}

const UserHome = { template: '<div>Home</div>' }
const UserProfile = { template: '<div>Profile</div>' }
const UserPosts = { template: '<div>Posts</div>' }

const router = new VueRouter({
  routes: [
    { path: '/user/:id', component: User,
      children: [
        // UserHome will be rendered inside User's <router-view>
        // when /user/:id is matched
        { path: '', component: UserHome },
				
        // UserProfile will be rendered inside User's <router-view>
        // when /user/:id/profile is matched
        { path: 'profile', component: UserProfile },

        // UserPosts will be rendered inside User's <router-view>
        // when /user/:id/posts is matched
        { path: 'posts', component: UserPosts }
      ]
    }
  ]
})

const app = new Vue({ router }).$mount('#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">
  <p>
    <router-link to="/user/foo">/user/foo</router-link>
    <router-link to="/user/foo/profile">/user/foo/profile</router-link>
    <router-link to="/user/foo/posts">/user/foo/posts</router-link>
  </p>
  <router-view></router-view>
</div>