Vue子路由器:url更改但显示父组件
Posted
技术标签:
【中文标题】Vue子路由器:url更改但显示父组件【英文标题】:Vue children Router: url changed but display parent component 【发布时间】:2018-04-18 08:23:06 【问题描述】:我正在学习 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
]
]
)
我尝试不使用子路由器,它会正确显示组件,就像注释的代码一样。
我很困惑,请帮助我。
【问题讨论】:
查看此链接可能会对您有所帮助:***.com/a/47074043/2815635 【参考方案1】:在您的Index
组件中,您需要添加<router-view></router-view>
。从 vue-router docs 中查看这个 working example。
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>
【讨论】:
以上是关于Vue子路由器:url更改但显示父组件的主要内容,如果未能解决你的问题,请参考以下文章