반응형
참고 사이트
https://next.router.vuejs.org/guide/essentials/dynamic-matching.html#catch-all-404-not-found-route
Dynamic Route Matching with Params | Vue Router
Dynamic Route Matching with Params Very often we will need to map routes with the given pattern to the same component. For example we may have a User component which should be rendered for all users but with different user IDs. In Vue Router we can use a d
next.router.vuejs.org
404오류를 보여줄 페이지 생성
코드
<template>
<div class="not-found">
<div class="status">
404
</div>
<div class="message">
Page Not Found
</div>
</div>
</template>
<style lang="scss" scoped>
@import "~/scss/main";
.not-found {
line-height: 1.2;
text-align: center;
font-family: "Oswald", sans-serif;
padding: 80px 20px;
.status {
font-size: 160px;
color: $primary;
}
.message {
font-size: 50px;
}
}
</style>
기존 index.js 파일에 연결
// 라우터 패키지 가져오기
import { createRouter, createWebHashHistory} from 'vue-router'
// 라우터를 위해 컴포넌트 가져오기
import Home from './Home'
import About from './About'
import Movie from './Movie'
import NotFound from './NotFound'
// 내보내기
export default createRouter({
// Hash
// https://google.com/#/search
// hash 모드를 사용(중간의 /#/이 추가된다.), 페이지를 새로고침하였을때 404에러를 방지해준다.
// history 모드도 있지만 간단한 처리를 위해서 hash를 사용하였다.(서버쪽에 설정을 추가해 줘야한다.)
history: createWebHashHistory(),
// pages: 페이지를 관리할때 사용되는 옵션
// https://google.com/
// url에 입력값에 따라 실행될 컴포넌트 지정
routes: [
{
path: '/',
component: Home
},
{
path: '/about',
component: About
},
{
path: '/movie/:id',
component: Movie
},
{
// 모든 경로
path: '/:notFound(.*)',
component: NotFound
}
]
})
화면 확인
반응형
'프론트엔드 > Vue.js' 카테고리의 다른 글
검색 정보 초기화 및 페이지 전환 스크롤 위치 복구 (0) | 2022.01.13 |
---|---|
반복적인 내용을 효율적으로 관리하는 방법: Vuex Helpers (0) | 2022.01.13 |
로딩 화면 만들기 (0) | 2021.09.27 |
container 너비 사용자 지정 (0) | 2021.09.23 |
비동기 - API 비동기 처리 연습 (0) | 2021.09.13 |