Skip to content

Route Meta

Route meta (RouteMeta) is used to attach custom properties to routes, such as page title, permission requirements, TabBar identification, etc.

RouteMeta Interface

ts
interface RouteMeta {
  title?: string
  isTab?: boolean
  requireAuth?: boolean
  [key: string]: unknown
}

Built-in Fields

title

Page title, can be used in afterEach hooks to dynamically set the navigation bar title:

ts
const routes = [
  { path: 'pages/index/index', name: 'home', meta: { title: 'Home' } },
  { path: 'pages/about/about', name: 'about', meta: { title: 'About Us' } }
]

router.afterEach((to) => {
  if (to.meta.title) {
    uni.setNavigationBarTitle({ title: to.meta.title as string })
  }
})

isTab

Identifies whether the page is a TabBar page. The router automatically selects the navigation API based on this field:

  • isTab: trueuni.switchTab
  • isTab: false or not set → uni.navigateTo / uni.redirectTo
ts
const routes = [
  { path: 'pages/user/user', name: 'user', meta: { isTab: true } }
]

// Automatically uses uni.switchTab
router.push({ name: 'user' })

::: important isTab must be consistent with the tabBar.list declaration in pages.json. If a page is a TabBar page but isTab: true is not set, navigation will use uni.navigateTo instead of uni.switchTab, causing navigation failure. :::

requireAuth

Identifies whether the page requires login authentication, commonly used with route guards:

ts
const routes = [
  { path: 'pages/profile/profile', name: 'profile', meta: { requireAuth: true } }
]

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth && !isLoggedIn()) {
    next({ name: 'login' })
  } else {
    next()
  }
})

Custom Extension Fields

RouteMeta supports adding arbitrary custom fields through index signatures:

ts
const routes = [
  {
    path: 'pages/article/article',
    name: 'article',
    meta: {
      title: 'Article Detail',
      requireAuth: false,
      keepAlive: true,
      pageType: 'content',
      permissions: ['read', 'comment']
    }
  }
]

Access custom fields in guards:

ts
router.beforeEach((to, from, next) => {
  const permissions = to.meta.permissions as string[] | undefined
  if (permissions && !hasPermissions(permissions)) {
    next(false)
  } else {
    next()
  }
})

TIP

Type assertions are needed when accessing custom fields because the index signature value is unknown type.

Accessing Meta

In Components

ts
import { useRoute } from '@meng-xi/uni-router'

const route = useRoute()
console.log(route.meta.title)
console.log(route.meta.isTab)

In Guards

ts
router.beforeEach((to, from, next) => {
  console.log(to.meta.requireAuth)
  next()
})

Via Router Instance

ts
router.currentRoute.meta.title

Released under the MIT License.