일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- 코드로배우는스프링웹프로젝트
- 스프링부트핵심가이드
- resttemplate
- 서버설정
- 코드로배우는스프링부트웹프로젝트
- 구멍가게코딩단
- 자바편
- 알파회계
- Kernighan의 C언어 프로그래밍
- 데비안
- GIT
- 처음 만나는 AI 수학 with Python
- network configuration
- 리눅스
- d
- 티스토리 쿠키 삭제
- 선형대수
- 자료구조와함께배우는알고리즘입문
- 스프링 시큐리티
- baeldung
- 친절한SQL튜닝
- 처음 만나는 AI수학 with Python
- 이터레이터
- /etc/network/interfaces
- ㅒ
- 목록처리
- iterator
- 자료구조와 함께 배우는 알고리즘 입문
- 네트워크 설정
- 페이징
- Today
- Total
bright jazz music
blog17 : 글 작성화면 만들기 2 본문
1. Vue component의 대략적인 사용법알아보기
<!--App.vue-->
<script setup lang="ts">
import { RouterLink, RouterView } from "vue-router";
// import HelloWorld from "./components/HelloWorld.vue";
</script>
<template>
<header>
<nav>
<RouterLink to="/">Home</RouterLink>
<RouterLink to="/write">글 작성</RouterLink> <!-- 글 작성 -->
</nav>
</header>
<RouterView />
</template>
<style scoped>
</style>
<!-- WriteView.vue -->
<script setup lang="ts">
import {ref} from "vue"; //ref를 사용하기 위해 import
// const count = 0 //변수 선언: 반응형 변수가 아니다. 보통은 ref나 reactive를 사용해서 반응형 변수를 만들어 준다.
const count = ref(0) //ref를 사용하여 만든 반응현 변수
</script>
<template>
<p>안녕하세요</p>
<p>{{count}}</p> <button @click="count += 1">버튼을 눌러주세요</button> <!-- @: vue용 함수 호출인 듯 -->
</template>
<style>
</style>
부트스트랩 또는 element ui라는 CSS 프레임워크를 사용하면 좀 더 예쁘게 꾸밀 수 있다.
여기까지가 기본적인 내용이었다. 이제 WriteView.vue 파일의 <template></template> 태그 내부의 내용을 모두 지운다.
2. 본격
<!-- WriteView.vue -->
<script setup lang="ts">
import {ref} from "vue"; //ref를 사용하기 위해 import
const count = ref(0)
</script>
<template>
<div>
<input type="text" placeholder="제목을 입력해 주세요">
</div>
<div>
<textarea rows="15"></textarea>
</div>
<button>글 작성 완료</button>
</template>
<style>
</style>
element plus 설치
https://element-plus.org/en-US/guide/installation.html#hello-world
뷰 프로젝트가 위치한 디렉토리로 이동
$ npm install element-plus --save
이제 element plus에 대한 컴포넌트를 사용할 수 있다.
추가적으로 부트스트랩 css 설치 (유틸용 css를 사용할 것이다)
npm install bootstrap@v5.2.3
//main.ts
import { createApp } from "vue";
import { createPinia } from "pinia";
import App from "./App.vue";
import router from "./router";
import ElementPlus from 'element-plus' //element plus 추가
import 'element-plus/dist/index.css' //element plus 추가
import "bootstrap/dist/css/bootstrap-utilities.css" //bootstrap css
// import "./assets/main.css"; 사용x
const app = createApp(App); //vue.js 앱을 맨들고
app.use(createPinia()); //필요한 프러그인 사용처리
app.use(router);
app.use(ElementPlus) //element plus 추가
app.mount("#app"); //#app으로 런치 ==> index.html 렌더링하고 거기에 App.vue 컴포넌트를 렌더링
<!-- WriteView.vue -->
<script setup lang="ts">
import {ref} from "vue"; //ref를 사용하기 위해 import
</script>
<template>
<div>
<input type="text" placeholder="제목을 입력해 주세요"/>
</div>
<div>
<div>
<textarea rows="15"></textarea>
</div>
<div>
<button>글 작성 완료</button>
</div>
</div>
</template>
<style>
</style>
element plus(el) 과 bootstrap utility css(mt-2)를 사용했을 때
<!-- WriteView.vue -->
<script setup lang="ts">
import {ref} from "vue"; //ref를 사용하기 위해 import
</script>
<template>
<div>
<el-input type="text" placeholder="제목을 입력해 주세요"/>
</div>
<div class="d-flex align-items-center">
<div>
<el-input type="textarea" rows="15"></el-input>
</div>
<div class="mt-2">
<el-button type="primary">글 작성 완료</el-button>
</div><!-- WriteView.vue -->
<script setup lang="ts">
import {ref} from "vue"; //ref를 사용하기 위해 import
</script>
<template>
<div class="mt-2">
<el-input placeholder="제목을 입력해 주세요"/>
</div>
<div class="mt-2">
<el-input type="textarea" rows="15"></el-input>
</div>
<div class="mt-2">
<el-button type="primary">글 작성 완료</el-button>
</div>
</template>
<style>
</style>
</div>
</template>
<style>
</style>
반응형 변수 title과 content를 선언하고 이들을 값을 입력 태그의 v-model 값으로 설정한다.
<!-- WriteView.vue -->
<script setup lang="ts">
import {ref} from "vue"; //ref를 사용하기 위해 import
const title = ref("") //입력받은 값을 저장할 변수 선언
const content = ref("") //입력받은 값을 저장할 변수 선언
</script>
<template>
<div class="mt-2">
<!-- 선언한 title, content 변수를 아래 v-model 태그에 넣는다.-->
<el-input v-model="title" placeholder="제목을 입력해 주세요"/>
</div>
<div class="mt-2">
<el-input v-model="content" type="textarea" rows="15"></el-input>
</div>
<div class="mt-2">
<el-button type="primary">글 작성 완료</el-button>
</div>
</template>
<style>
</style>
이제 '글 작성 완료' 버튼을 누르면 포스트가 저장되도록 만들어야 한다.
우선 테스트
<!-- WriteView.vue -->
<script setup lang="ts">
import {ref} from "vue"; //ref를 사용하기 위해 import
const title = ref("") //입력받은 값을 저장할 변수 선언
const content = ref("") //입력받은 값을 저장할 변수 선언
const write = function() {
// console.log(title, content) ref로 감싸져 있음.
console.log(title.value, content.value)//콘솔에 직접 찍힘
}
</script>
<template>
<div class="mt-2">
<!-- 선언한 title, content 변수를 아래 v-model 태그에 넣는다.-->
<el-input v-model="title" placeholder="제목을 입력해 주세요"/>
</div>
<div class="mt-2">
<el-input v-model="content" type="textarea" rows="15"></el-input>
</div>
<div class="mt-2">
<el-button type="primary" @click="write()">글 작성 완료</el-button> <!--눌렀을 때 write() 함수 호출 -->
</div>
</template>
<style>
</style>
콘솔에 찍히는 모습을 볼 수 있다.
이걸 보내려면 AXIOS라는 라이브러리를 사용한다.
https://axios-http.com/kr/docs/intro
오류 발생
CommonJs형태가 아니었기 때문. 따라서 import axios from 'axios' 로 임포트
<!-- WriteView.vue -->
<script setup lang="ts">
import {ref} from "vue"; //ref를 사용하기 위해 import
// const axios = require('axios').default; 오류발생 :CommonJS형태가 아니기 때문.
import axios from 'axios'// 따라서 이렇게 import
const title = ref("") //입력받은 값을 저장할 변수 선언
const content = ref("") //입력받은 값을 저장할 변수 선언
const write = function() {
axios.get("https://google.com")
}
</script>
<template>
<div class="mt-2">
<!-- 선언한 title, content 변수를 아래 v-model 태그에 넣는다.-->
<el-input v-model="title" placeholder="제목을 입력해 주세요"/>
</div>
<div class="mt-2">
<el-input v-model="content" type="textarea" rows="15"></el-input>
</div>
<div class="mt-2">
<el-button type="primary" @click="write()">글 작성 완료</el-button> <!--눌렀을 때 write() 함수 호출 -->
</div>
</template>
<style>
</style>
CORS문제가 발생하는 이유.
==>
우리가 만든 localhost:8080 에 쏴도 마찬가지다.
CORS에러 해결하기.
//WebConfig.java
package com.endofma.blog.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("http://localhost:5173");
}
}
현재 로컬 vue의 포트가 5173이다.
'글 작성완료' 버튼을 눌러서 http://localhost:8080에 get으로 접근해보자.
글 작성하기
//WriteView.vue
const write = function() {
axios.post("http://localhost:8080/posts") //글 등록 api
}
전문
<!-- WriteView.vue -->
<script setup lang="ts">
import {ref} from "vue"; //ref를 사용하기 위해 import
// const axios = require('axios').default; 오류발생 :CommonJS형태가 아니기 때문.
import axios from 'axios'// 따라서 이렇게 import
const title = ref("") //입력받은 값을 저장할 변수 선언
const content = ref("") //입력받은 값을 저장할 변수 선언
//WriteView.vue
const write = function() {
axios.post("http://localhost:8080/posts",
//RequestBody
{
title: title.value,
content: content.value
});
}
</script>
<template>
<div class="mt-2">
<!-- 선언한 title, content 변수를 아래 v-model 태그에 넣는다.-->
<el-input v-model="title" placeholder="제목을 입력해 주세요"/>
</div>
<div class="mt-2">
<el-input v-model="content" type="textarea" rows="15"></el-input>
</div>
<div class="mt-2">
<el-button type="primary" @click="write()">글 작성 완료</el-button> <!--눌렀을 때 write() 함수 호출 -->
</div>
</template>
<style>
</style>
'Projects > blog' 카테고리의 다른 글
blog19 : 글 리스트 화면 만들기 (0) | 2023.02.03 |
---|---|
blog18 : CORS문제 해결 (server.proxy) (0) | 2023.02.03 |
blog17 : 글 작성화면 만들기 1 (기초) (0) | 2023.02.01 |
blog16. Vue.js 설치 (0) | 2023.01.30 |
blog15 : Git 자주 쓰는 명령어 예제 2 (0) | 2023.01.30 |