So i have a side navbar and some link in it. The entry point is the homepage. The problem is, when i'm trying to change router-view using router-link in my side navbar, the router-view doesn't change, but the path has change.
So here's my route path on my router.js:
{
path: '/',
ponent: Home,
children: [
{
path: ':name',
name: 'theTask',
props: true,
ponent: TodoList
}
]
},
],
And here's my App.vue
<div class="container">
<div class="main__content">
<div class="sidenav">
<aside class="menu">
<ul class="menu-list">
<li><router-link :to="{ name: 'theTask', params: {name: 'today'} }">Today</router-link></li>
<li><router-link :to="{ name: 'home' }">Next 7 Days</router-link></li>
</ul>
</aside>
</div>
<div class="content">
<router-view></router-view>
</div>
</div>
</div>
It rendered the homepage, but when i'm clicking router-link to the child route, it doesn't change the router-view, only the router link.
So i have a side navbar and some link in it. The entry point is the homepage. The problem is, when i'm trying to change router-view using router-link in my side navbar, the router-view doesn't change, but the path has change.
So here's my route path on my router.js:
{
path: '/',
ponent: Home,
children: [
{
path: ':name',
name: 'theTask',
props: true,
ponent: TodoList
}
]
},
],
And here's my App.vue
<div class="container">
<div class="main__content">
<div class="sidenav">
<aside class="menu">
<ul class="menu-list">
<li><router-link :to="{ name: 'theTask', params: {name: 'today'} }">Today</router-link></li>
<li><router-link :to="{ name: 'home' }">Next 7 Days</router-link></li>
</ul>
</aside>
</div>
<div class="content">
<router-view></router-view>
</div>
</div>
</div>
It rendered the homepage, but when i'm clicking router-link to the child route, it doesn't change the router-view, only the router link.
Share Improve this question asked Jun 1, 2019 at 17:29 cantdocppcantdocpp 1,13613 silver badges24 bronze badges3 Answers
Reset to default 3The real question is what is happening under the Hood.
routes.js
var routes = [{
path: '/',
name: 'home', // missed this one
ponent: Home,
children: [
{
path: ':name',
name: 'theTask',
props: true,
ponent: TodoList
}
]
}]
the parent is
Home
and the child isTodoList
the route for parent is/
and route for the child is/:name
. you are trying to change the view true. and it's changing. But you can't see it. because there is norouter-view
in the parent of the ponent you are trying to change.By default, the parent is
App.vue
for every object in the routes array(if you don't change it otherwise). SoParent
of ponentHome
isApp.vue
. But the parent ofTodoList
is ponentHome
. I am sure yourHome.vue
doesn't have<router-view>
. When you are trying to render ponentTodoList
it is rendering its parent and then finding<router-view>
in its parent and trying to render itself there. But there isn't. And even if you add<router-view>
in theHome.vue
what you will have is bothHome
andTodoList
rendering.What I presume you are trying to do is to render
Home
andTodoList
render separately onApp.vue
place. there are 2 options.
in routes.js
file add 2 paths one for /
and another for /:name
var routes = [
{
path: '/',
name: 'home',
ponent: Home,
},
{
path: '/:name',
name: 'theTask',
props: true,
ponent: TodoList
}];
have a base ponent(if you really need it) and add 2 children.
var routes = [{
path: '/',
ponent: Base,
children: [
{
path: '/',
name: 'home',
ponent: Home
},
{
path: ':name',
name: 'theTask',
props: true,
ponent: TodoList
}
]
}]
Have a closer look at Evan You
's example here
I am not sure about the use case but most likely you don't need to use children
in your routes. That is meant for ponents that live ON the original page as their parent. If you just want a different ponent on a different page, you should define them as individual routes:
{
path: '/',
name: 'Home',
ponent: Home
},
{
path: '/:name',
name: 'theTask', // although IMO this should be called something like "TodoList"
ponent: TodoList
}
You can then use the Vue Developer Tools (for Chrome) to inspect and see which route is currently being rendered on your page.
In this case you should provide an empty subroute path. I have created a fiddle for the same here. This is as per the vue-router
documentation.
{ path: '/',
ponent: Base,
children: [{
path: '',
name: 'Home',
ponent: Home
},{
path: ':name',
name: 'theTask',
props: true,
ponent: Foo
}]
}
]
})
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745263292a4619311.html
评论列表(0条)