Reputation: 472
In this question there is a great example of a solution. But it is not working when I try to use route via name.
I tried something like that:
<v-tabs v-model="active">
<v-tab v-for="tab of tabs" :key="tab.id" :to="{ name: tab.route_name }">
{{ tab.name }}
</v-tab>
<v-tab-item v-for="tab of tabs" :key="tab.id" :value="{ name: tab.route_name }">
<router-view></router-view>
</v-tab-item>
</v-tabs>
data() {
return {
active: '',
tabs: [
{ id: 1, name: "Task", route_name: 'task' },
{ id: 2, name: "Project", route_name: 'project' }
]
};
}
const routes = [
{
path: '/task',
name: 'task',
component: Task
},
{
path: '/project',
name: 'project',
component: Project
},
];
It just breaks, because :value can't be Object.
I create working jsfiddle and breaking version for play.
P.S. I can't add comments to answers, so I created a new question.
Update: Temporary solution:
I use the manual resolve of the router, like:
<v-tabs v-model="active">
<v-tab v-for="tab of tabs" :key="tab.id" :to="tab.route">
{{ tab.name }}
</v-tab>
<v-tab-item v-for="tab of tabs" :key="tab.id" :value="tab.route">
<router-view></router-view>
</v-tab-item>
</v-tabs>
data: {
active: '',
},
computed: {
tabs() {
return [{
id: 1,
name: "Task",
route: this.routeResolve('task')
},
{
id: 2,
name: "Project",
route: this.routeResolve('project')
}
]
}
},
methods: {
routeResolve(name) {
return this.$router.resolve({
name: name,
}).location.path
},
},
Upvotes: 4
Views: 4532
Reputation: 472
I noticed that the above solutions have one problem. Next code is loop:
<v-tab-item v-for="tab of tabs" :key="tab.id" :value="tab.route">
<router-view></router-view>
</v-tab-item>
It is mean, that each router will be duplicated N-time (where N is tabs count). When I try fix it I found that we can just use next code as solution:
<v-tabs v-model="active">
<v-tab v-for="tab of tabs" :key="tab.id" :to="{ name: tab.route_name }">
{{ tab.name }}
</v-tab>
</v-tabs>
<router-view></router-view>
...
Upvotes: 2