简体   繁体   中英

How to use Vuetify tabs with vue-router via router name

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.

PS 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
    },
  },

jsfiddle example

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>

...

jsfiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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