[FIXED] How to get list all name of routes?

Issue

export const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Main',
    component: () => import('~/pages/Main.vue'),
    children: [],
  },
  {
    path: '/auth',
    name: 'Auth',
    component: () => import('~/pages/Auth.vue'),
    meta: {
      isAuthPage: true,
    },
  },
  {
    path: '/movies',
    name: 'Movies',
    component: () => import('~/pages/Movies.vue'),
    meta: {
      isAuthPage: true,
    },
  },
  {
    path: '/tv-shows',
    name: 'TVShows',
    component: () => import('~/pages/TVShows.vue'),
    meta: {
      isAuthPage: true,
    },
  },
]

I expected type Pages = 'Main' | 'Auth' | 'Movies' | 'TVShows

Solution

What do you mean by you expected? Do you want to create this type? You could do that like this:

export const routes = [
  {
    path: '/',
    name: 'Main',
    component: () => import('~/pages/Main.vue'),
    children: [],
  },
  {
    path: '/auth',
    name: 'Auth',
    component: () => import('~/pages/Auth.vue'),
    meta: {
      isAuthPage: true,
    },
  },
  {
    path: '/movies',
    name: 'Movies',
    component: () => import('~/pages/Movies.vue'),
    meta: {
      isAuthPage: true,
    },
  },
  {
    path: '/tv-shows',
    name: 'TVShows',
    component: () => import('~/pages/TVShows.vue'),
    meta: {
      isAuthPage: true,
    },
  },
] as const

type Pages = typeof routes[number]["name"]

Answered By – oliverwebr

Answer Checked By – Mary Flores (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published