๐ค ์์งlog ๐ค
[VUE] Vue๋ก ๋ง๋ ๊ฒ์ํ์ CRUD ๊ตฌํํ๊ธฐ ๋ณธ๋ฌธ
๊ฒ์ํ css๋ bootstrap์ ์ฌ์ฉํ์๊ณ ์ด ๊ธ์ crud๊ณผ์ ๋ง ์ ๋ฆฌํ ๊ธ ์๋ฏธ๋ฑ.. !๐
๐ json-server
RestApi๋ json-server์ ์ฌ์ฉํ์ต๋๋ค !
// ๊ฐ๋ฐํ๊ฒฝ ์ค์น: -D
npm install -D json-server
// ์ ์ญ์ ์ผ๋ก ์ค์น: -g
npm install -g json-server
// ๋ก์ปฌ์ ์ค์นํ ๊ฒฝ์ฐ( -D ์ต์
์ค์น)
npx json-server --watch db.json
// ์ ์ญ์ ์ผ๋ก ์ค์นํ ๊ฒฝ์ฐ ( -g ์ต์
์ค์น)
json-server --watch db.json
๋ฃจํธ ํด๋ ์์ db.json ํด๋๊ฐ ์์ฑ๋๊ณ , ์์ ์ฐ์ ์์๋ก ํ ์คํธ์ฉ ๋ฐ์ดํฐ ๋ฃ์ด์ฃผ์๋ฉด ๋ฉ๋๋ค !
// db.json
{
"posts": [
{
"id": 1,
"title": "์ ๋ชฉ1",
"content": "๋ด์ฉ1",
"createdAt": "2023-01-01"
},
{
"id": 2,
"title": "์ ๋ชฉ2",
"content": "๋ด์ฉ2",
"createdAt": "2023-03-17"
},
{
"id": 3,
"title": "์ ๋ชฉ3",
"content": "๋ด์ฉ3",
"createdAt": "2023-04-26"
},
{
"id": 4,
"title": "์ ๋ชฉ4",
"content": "๋ด์ฉ4",
"createdAt": "2023-10-20"
},
{
"id": 5,
"title": "์ ๋ชฉ5",
"content": "๋ด์ฉ5",
"createdAt": "2023-12-15"
}
]
}
๋ฐ์ดํฐ ๋ฃ์ด ์ฃผ์ ํ ๋ค์ ์๋ ๋ช ๋ น์ด๋ก ํ์ธํด ๋ณผ ์ ์๋๋ฐ
์ด๋ ! ํฌํธ๋ฅผ ์ง์ ํ ์ ์์ต๋๋ค. ์ํ์๋ ํฌํธ ์ง์ ํ์๊ณ localhost:4000/posts์ผ๋ก ๋ค์ด๊ฐ์๋ฉด ๋ณธ์ธ์ด ์ ์ฅํ json๋ฐ์ดํฐ๋ฅผ ๋ณผ์์์ต๋๋ค !
npx json-server --watch db.json --port 4000
๐ axios ์ค์น
npm i axios
๐ src/api(ํด๋)/posts.ts
import axios from 'axios'
export function getPosts() {
return axios.get('http://localhost:4000/posts')
}
export function getPostById(id: number) {
return axios.get(`http://localhost:4000/posts/${id}`)
}
export function createPost(data: any) {
return axios.post('http://localhost:4000/posts', data)
}
export function updatePost(id: number, data: any) {
return axios.put(`http://localhost:4000/posts/${id}`, data)
}
export function deletePost(id: number) {
return axios.delete(`http://localhost:4000/posts/${id}`)
}
๐ Create
<template>
<div>
<h2>๊ฒ์๊ธ ๋ฑ๋ก</h2>
<hr class="my-4" />
<form @submit.prevent="save">
<div class="mb-3">
<label for="title" class="form-label">์ ๋ชฉ</label>
<input v-model="form.title" type="text" class="form-control" id="title" />
</div>
<div class="mb-3">
<label for="content" class="form-label">๋ด์ฉ</label>
<textarea v-model="form.content" class="form-control" id="content" rows="3"></textarea>
</div>
<div class="pt-4">
<button type="button" class="btn btn-outline-dark me-2" @click="goListPage">๋ชฉ๋ก</button>
<button class="btn btn-primary">์ ์ฅ</button>
</div>
</form>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { createPost } from '../../api/posts'
const router = useRouter()
const form = ref({
title: null,
content: null
})
const save = () => {
try {
createPost({
...form.value,
createdAt: Date.now()
})
router.push({ name: 'PostList' })
} catch (error) {
console.error(error)
}
}
const goListPage = () => router.push({ name: 'PostList' })
</script>
<style></style>
๐ List
<template>
<h2>๊ฒ์๊ธ ๋ชฉ๋ก</h2>
<hr class="my-4" />
<div class="row g-3">
<div v-for="post in posts" :key="post.id" class="col-4">
<PostItem
:title="post.title"
:content="post.content"
:created-at="post.createdAt"
@click="goPage(post.id)"
></PostItem>
</div>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import PostItem from '../../components/posts/PostItem.vue'
import { getPosts } from '../../api/posts'
const router = useRouter()
const posts = ref([])
const fetchPosts = async () => {
try {
const { data } = await getPosts()
posts.value = data
} catch (error) {
console.error(error)
}
}
fetchPosts()
const goPage = (id) => {
router.push({
name: 'PostDetail',
params: { id }
})
}
</script>
<style></style>
๐ Detail
<template>
<h2>{{ form.title }}</h2>
<p>{{ form.content }}</p>
<p class="text-muted">{{ form.createdAt }}</p>
<hr class="my-4" />
<div class="row g-2">
<div class="col-auto">
<button class="btn btn-outline-dark">์ด์ ๊ธ</button>
</div>
<div class="col-auto">
<button class="btn btn-outline-dark">๋ค์๊ธ</button>
</div>
<div class="col-auto me-auto"></div>
<div class="col-auto">
<button class="btn btn-outline-dark" @click="goListPage">List</button>
</div>
<div class="col-auto">
<button class="btn btn-outline-primary" @click="goEditPage">Edit</button>
</div>
<div class="col-auto">
<button class="btn btn-outline-danger" @click="remove">Delete</button>
</div>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { getPostById, deletePost } from '../../api/posts'
const route = useRoute()
const router = useRouter()
const id = route.params.id
const form = ref({})
const fetchPost = async () => {
try {
const { data } = await getPostById(id)
form.value = { ...data }
} catch (error) {
console.error(error)
}
}
fetchPost()
const remove = async () => {
try {
if (confirm('์ญ์ ํ์๊ฒ ์ต๋๊น?')) {
await deletePost(id)
router.push({ name: 'PostList' })
}
} catch (error) {
console.error(error)
}
}
const goListPage = () => router.push({ name: 'PostList' })
const goEditPage = () => router.push({ name: 'PostEdit', params: { id } })
</script>
<style></style>
๐ Edit
<template>
<div>
<h2>๊ฒ์๊ธ ์์ </h2>
<hr class="my-4" />
<form @submit.prevent="edit">
<div class="mb-3">
<label for="title" class="form-label">์ ๋ชฉ</label>
<input v-model="form.title" type="text" class="form-control" id="title" />
</div>
<div class="mb-3">
<label for="content" class="form-label">๋ด์ฉ</label>
<textarea v-model="form.content" class="form-control" id="content" rows="3"></textarea>
</div>
<div class="pt-4">
<button type="button" class="btn btn-outline-danger me-2" @click="goDetailPage">
์ทจ์
</button>
<button class="btn btn-primary">์์ </button>
</div>
</form>
</div>
</template>
<script setup>
import { useRoute, useRouter } from 'vue-router'
import { ref } from 'vue'
import { getPostById, updatePost } from '../../api/posts'
const route = useRoute()
const router = useRouter()
const id = route.params.id
const form = ref({
title: null,
content: null
})
const fetchPost = async () => {
try {
const { data } = await getPostById(id)
setForm(data)
} catch (error) {
console.error(error)
}
}
const setForm = ({ title, content }) => {
form.value.title = title
form.value.content = content
}
fetchPost()
const edit = async () => {
try {
await updatePost(id, { ...form.value })
router.push({ name: 'PostDetail', params: { id } })
} catch (error) {
console.error(error)
}
}
const goDetailPage = () => router.push({ name: 'PostDetail', params: { id } })
</script>
<style></style>
โค๏ธ ์ ์ฒด ์ฝ๋ :
โค๏ธ ๋์ ์ฃผ์ ๋ถ :
728x90
'๐ Vue' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[VUE] ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ & v-for ๋ฐ๋ณต๋ฌธ & ์ด๋ฒคํธ ํธ๋ค๋ฌ (0) | 2023.10.20 |
---|