// views/NotFound404.vue
<template>
<div>
<h1>404 NOT FOUND</h1>
</div>
</template>
<script>
export default {
name: '404NotFound',
}
</script>
// router/index.js
import NotFound404 from '@/views/NotFound404'
const routes = [
...
{
path: '/404',
name: 'NotFound404',
component: NotFound404
},
]
// router/index.js
import NotFound404 from '@/views/NotFound404'
const routes = [
...
{
path: '*',
redirect: '/404'
},
]
articles/1/
로 요청을 보냈지만, 1번 게시글이 삭제된 상태💡 Dog API 문서(https://dog.ceo/dog-api/) 를 참고하여 동적 인자로 강아지 품종을 전달해 품종에 대한 랜덤 이미지를 출력하는 페이지를 만들어보기
$ npm i axios
// views/DogView.vue
<template>
<div>
</div>
</template>
<script>
export default {
name: 'DogView',
}
</script>
<style>
</style>
// router/index.js
import DogView from '@/views/DogView'
const routes = [
...,
{
path: '/dog/:breed',
name: 'dog',
component: DogView
},
{
path: '*', // 위에 존재하지 않는 모든 것
redirect: '/404'
},
]
// views/DogView.vue
<template>
<div>
<img v-if="imgSrc" :src="imgSrc" alt=""><br>
</div>
</template>
<script>
import axios from 'axios'
export default {
name: 'DogView',
data() {
return {
imgSrc: null,
}
},
methods: {
getDogImage(){
const breed = this.$route.params.breed // 품종 가져오기
const dogImageSearchURL = `https://dog.ceo/api/breed/${breed}/images/random`
axios({
method:'get',
url: dogImageSearchURL
})
.then((response) =>{
console.log(response)
const imgSrc = response.data.message
this.imgSrc = imgSrc
})
.catch((error) => console.log(error))
}
},
created() {
this.getDogImage()
}
}
</script>
<style>
</style>
// views/DogView.vue
<template>
<div>
<p v-if="!imgSrc">{{ message }}</p>
<img v-if="imgSrc" :src="imgSrc" alt=""><br>
</div>
</template>
<script>
import axios from 'axios'
export default {
name: 'DogView',
data() {
return {
imgSrc: null,
message: "로딩중..."
}
},
// views/DogView.vue
axios({
method:'get',
url: dogImageSearchURL
})
.then((response) =>{
console.log(response)
const imgSrc = response.data.message
this.imgSrc = imgSrc
})
.catch((error) => {
this.message=`${this.$route.params.breed}는 없는 품종입니다`
console.log(error)
})
/dog/abc
에 접속// views/DogView.vue
axios({
method:'get',
url: dogImageSearchURL
})
.then((response) =>{
console.log(response)
const imgSrc = response.data.message
this.imgSrc = imgSrc
})
.catch((error) => {
this.$router.push('/404')
console.log(error)
})
https://github.com/mjieun0956/TIL/tree/master/Vue/12.%20404%20Not%20Found/404-not-found