route's meta field
라우트에 meta
라는 속성 객체를 사용하여 임의의 정보를 추가하면, route location과 navigation guard에서 접근할 수 있습니다.
const routes = [
{
path: '/posts',
component: PostsLayout,
children: [
{
path: 'new',
component: PostsNew,
// only authenticated users can create posts
meta: { requiresAuth: true },
},
{
path: ':id',
component: PostsDetail,
// anybody can read a post
meta: { requiresAuth: false },
},
],
},
]
자! 그럼 접근을 할. 수. 있. 다. 라는것을 알았습니다.
그러면 어떻게 접근해야 할까요??
우선 routes
의 각 라우트 객체를 라우트 레코드(route record)
라고 하며, 중첩될 수 있습니다. 따라서, 매칭된 라우트는 둘 이상의 라우트 레코드와 매칭될 수 있습니다.
예를 들어 위에 두성된 라우트에서 /posts/new
에 접근했다고 가정해 봅시다. 그러면 부모 라우트 레코드(posts
)와 자식 라우트 레코드(new
)와 모두 일치합니다.
모든 라우트에 의해 일치된 라우트 기록은 route
객체에 route.matched
에 배열로 노출되게 됩니다. meta
의 경우 해당 배열을 순회하여 찾아내도 되지만, VueRouter에는 부모~자식간의 meta
필드의 비재귀적 병합인 route.meta
를 제공합니다.
이 말은 즉, 부모와 자식에 작성한 meta
필드 정보를 route.meta
를 통해 전부 꺼낼 수 있다는 말이 됩니다.
예를 들어보겠습니다.
const routes = [
{
path: '/router/:route',
name: 'router',
props: {
routeObject: {
a: 0,
b: '',
c: true,
},
route: true,
},
meta: { test1: true },
component: () => import('@/components/RouterStudy.vue'),
children: [{ path: '', name: 'routerNote', meta: { test2: false }, component: () => import('@/components/Note.vue'), props: true }],
},
]
router.afterEach((to, from, failure) => {
let route = useRoute()
console.log(route.meta)
console.log(to)
})
// console.log(route.meta)
{test1: true, test2: false}
// console.log(to)
fullPath: "/router/%EB%9D%BC%EC%9A%B0%ED%8A%B8"
hash: ""
href: "/router/%EB%9D%BC%EC%9A%B0%ED%8A%B8"
matched: (2) [{…}, {…}]
meta: {test1: true, test2: false}
name: "routerNote"
params: {route: '라우트'}
path: "/router/%EB%9D%BC%EC%9A%B0%ED%8A%B8"
query: {}
redirectedFrom: undefined
...
728x90
반응형
'FrameWorks > Vue' 카테고리의 다른 글
VueRouter의 Composition API 파헤치기 (0) | 2024.07.29 |
---|---|
VueRouter의 Data Fetching 파헤치기 (0) | 2024.06.27 |
VueRouter의 Navigation Guard 파헤치기 (0) | 2024.06.27 |
VueRouter의 Diffrenet History Modes 파헤치기 (0) | 2024.06.26 |
VueRouter의 Active Links 파헤치기 (0) | 2024.06.23 |
댓글