Vue3에서 Nested Routes를 활용하는 방법과 라우터 관련 함수($router.push, $router.go 등)를 알아봅니다.
/detail/0/author로 접속하면 Detail 페이지 내에 작가 소개(Author) 컴포넌트가, /detail/0/comment로 접속하면 댓글(Comment) 컴포넌트가 표시됩니다.router.js 설정const routes = [
{
path: '/detail/:id',
component: Detail,
children: [
{ path: 'author', component: Author },
{ path: 'comment', component: Comment },
]
}
]
/detail/:id 경로에 해당하는 Detail 컴포넌트를 기본으로 로드하고, 그 안에 children 배열을 통해 하위 경로(자식 라우트)를 설정합니다./detail/:id/author와 /detail/:id/comment 경로로 접근하면 각각 Author와 Comment 컴포넌트가 표시됩니다.<router-view><router-view></router-view> 태그를 추가합니다./detail/:id/author 접속 시 <router-view> 자리에 Author 컴포넌트가 보입니다.참고: 간단한 UI라면 탭 기능 방식으로 구현하는 것도 좋습니다.
$router.push$router.push('/detail/0');
/detail/0 경로로 이동하게 됩니다.<router-link> 대신 사용 가능하며, 변수나 동적 경로를 쉽게 적용할 수 있습니다.$router.go$router.go(-1); // 한 단계 뒤로가기
$router.go(1); // 한 단계 앞으로가기
$router.go(-2); // 두 단계 뒤로가기