์ฐธ๊ณ ๋ก Vue.js์ ๋ํด์ ์ ๋ชจ๋ฆ ๋๋ค.
์๋ฌด๊ฑฐ๋ ๋ง ํด๋ณด๋ ์ค์ด๊ณ ์ณ์ ์ ๋ณด์ธ์ง ์ ๋ ๋ชจ๋ฆ ๋๋ค!
๋ฒํผ์ ๋ง๋ค๊ณ
๊ทธ ๋ฒํผ์ ๋๋ฅด๋ฉด
ํ ์คํธ๊ฐ ๋์ค๋๋ก
๋ง๋ค ๊ฒ์ด๋ค.
1. ์คํ ํ๋ฉด
2. HelloView.vue
MainView.vue์ ์๋ Hello,Vue!! ํ ์คํธ ์ถ๋ ฅ ๋ถ๋ถ ์ฝ๋๋ฅผ HelloView.vue๋ก ์ฎ๊ฒจ์ค๋ค. ๋ง์ฝ ์๋กญ๊ฒ ํ๋ฉด์ ๋ง๋ ๋ค๋ฉด ๋ฉ์ธํ๋ฉด ์ฐฝ ํ๋์ ํ ์คํธ๋ฅผ ์ค์ ํ ์ฐฝ ํ๋๋ฅผ ๋ง๋ค๋ฉด ๋๋ค.
<script>
export default {
name: 'HelloVue',
props: {
showHello: {
type: Boolean,
required: true,
}
}
}
</script>
<template>
<div class="hello-vue">
<h1> HelloVue!! </h1>
</div>
</template>
<style scoped>
.hello-vue {
position: fixed;
top: 50%;
left: 80%;
transform: translate(-50%, -50%);
background: white;
color: olivedrab;
font-family: "Cafe24Meongi-B-v1.0";
}
</style>
๋๋จธ์ง๋ ๊ฑฐ์ ๋๊ฐ์๋ฐ, ํ ์คํธ ์์น๋ง left: 80%๋ก ์ฎ๊ฒจ์คฌ๊ณ <script>์ฝ๋๊ฐ ์ถ๊ฐ๋๋ค. <script> ์ฝ๋๋ง ์ดํด๋ณด๊ฒ ๋ค.
export default {
name: 'HelloVue',
props: {
showHello: {
type: Boolean,
required: true,
}
}
}
export๋ ์ธ๋ถ๋ก ๋ด๋ณด๋ผ๋ ์ด๋ค. import๋ ๋ฐ์์ฌ๋ ์ฐ๋ ๊ฒ์ผ๋ก export์ ๋ฐ๋๋๋ ๊ฐ๋ ์ด๋ค. default๋ ๊ฐ์ฒด๊ฐ ํ๋๋ง ์๋ ๊ฒฝ์ฐ ๋ถ์ธ๋ค. ํ์ฌ ์ฝ๋์์๋ ๊ฐ์ฒด ์ ์ฒด๋ฅผ HelloVue๋ผ๊ณ ๋ฑ๋กํ์ฌ ๋ด๋ณด๋ด๋๊น default๋ฅผ ๋ถ์๋ค.
props๋ ๋ถ๋ชจ ์ปดํฌ๋ํธ๊ฐ ์์์๊ฒ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ์๋จ์ด๋ค. ์ด ์ฝ๋์์๋ <script setpu>์ผ๋ก ์ค์ ํ์ง ์๊ณ ๊ทธ๋ฅ <script>๋ก ์ค์ ํ๋ค. ๊ทธ๋์ props ์ต์ ์ ์ฌ์ฉํ์ฌ props๋ฅผ ์ ์ธํ๋ค.
showHello๋ ์ ๋ฌ๋ฐ๋ prop์ ์ด๋ฆ์ด๋ค. ์ด ์ฝ๋์์ Hello,vue๋ผ๋ ํ ์คํธ๊ฐ ๋ฒํผ์ ๋๋ฅด๋ฉด ๋ณด์ด๊ณ ๋๋ฅด๊ธฐ ์ ์๋ ์ ๋ณด์ด๊ฒ ํ๊ธฐ ๋๋ฌธ์ type์ Boolean์ผ๋ก ์ค์ ํ์ฌ showHello๋ true์ false ๊ฐ๋ง ๋ฐ๋๋ก ํ๋ค. (true ๊ฐ์ด ์ค๋ฉด ๋ณด์ด๋๋ก ํ๊ณ , false ๊ฐ์ด ์ค๋ฉด ์ ๋ณด์ด๋๋ก ํ ๊ฑฐ๋๊น) required๋ฅผ true๋ก ์ค์ ํ์ฌ ๋ถ๋ชจ vue๊ฐ ๋ฐ๋์ showHello์ prop์ ๋ฐ์์ผ ์ ์ ์๋ํ๋๋ก ์ค์ ํ๋ค.
๊ทธ๋์ MainView์ ๊ฐ์ prop์ ๋ฐ์ ์ฝ๋๋ฅผ ์์ด๋ค. ๊ทธ๋ฐ๋ฐ ๋์์ ๋ฌธ์ ๊ฐ ์์๋ค!! ๊ณต์๋ฌธ์๋ฅผ ์ฐพ์๋ณด๋ Boolean์ ์์ธ์ ์ผ๋ก ๋ค๋ฅด๊ฒ ๋์ํ๋ค๊ณ ํ๋ค. Boolean ํ์ ์ ์ ์ธํ ํ์ ์ prop์ ๋ฐ์์ผ ๋์ํ๋ค.
Vue.js (์ถ์ฒ: https://ko.vuejs.org/guide/components/props)
· ๋ชจ๋ prop์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ํ ์ฌํญ์ด๋ฉฐ, required: true๊ฐ ์ง์ ๋ ๊ฒฝ์ฐ์๋ง ํ์์ ๋๋ค.
· Boolean์ด ์๋ ์ ํ์ prop์ด ์์ผ๋ฉด ๊ฐ์ undefined๊ฐ ๋ฉ๋๋ค.
· Boolean prop์ด ์์ผ๋ฉด false๋ก ๋ณํ๋ฉ๋๋ค. ์ด๋ฅผ ๋ณ๊ฒฝํ๋ ค๋ฉด default๋ฅผ ์ค์ ํ ์ ์์ต๋๋ค.
์: default: undefined๋ก ์ค์ ํ๋ฉด Boolean์ด ์๋ prop์ฒ๋ผ ๋์ํฉ๋๋ค.
· default ๊ฐ์ด ์ง์ ๋ ๊ฒฝ์ฐ, prop ๊ฐ์ด undefined๋ก ํด์๋๋ฉด(์ฆ, prop์ด ์๊ฑฐ๋ ๋ช ์์ ์ผ๋ก undefined๊ฐ ์ ๋ฌ๋ ๊ฒฝ์ฐ) ํด๋น ๊ฐ์ด
์ฌ์ฉ๋ฉ๋๋ค.
3. MainView.vue์ ๋ฒํผ ๋ง๋ค๊ธฐ
<template>
<div class="main-view">
<button class="btn" @click="btnHello" style="cursor: pointer"> Click </button>
</div>
</template>
main-view ์์ ๋ฒํผ์ ๋ง๋ ๋ค. <button>์ผ๋ก ์ฝ๊ฒ ๋ฒํผ์ ๋ง๋ค ์ ์๋ค. @click์ ํด๋ฆญ์ ์ด๋ค ๋์์ ์ผ์ด๋๋๋ก ๋ง๋ค๊ฑฐ๋ผ, ๊ทธ ๋์์ด ์ผ์ด๋ ์ด๋ฆ์ btnHello๋ผ๊ณ ์ ์ํ๋ค. cursor: pointer์ ๋ฒํผ์ ์ปค์๋ฅผ ๋๋ฉด ํด๋ฆญํ๋ ๋ชจ์์ผ๋ก ๋ฐ๋๋๋ก ์คํ์ผ์ ์ง์ ํ๋ค.
.btn {
position: absolute;
width: 80px;
height: 50px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 20px;
color: white;
background-color: black;
}
position: absolute๋ ๊ฐ์ฅ ๊ฐ๊น์ด ๋ถ๋ชจ ์์๋ฅผ ์ฐพ์์ ๊ทธ ์์์ ์์น๋ฅผ ๊ธฐ์ค์ผ๋ก ์์น๋ฅผ ์ก๋๋ค. main-view์ position์ relative๊ณ , width: 100vw์ height:100vh๋ฅผ ์คฌ๋ค. ์ฃผ๋ก ๋ถ๋ชจ-์์ ์์๋ฅผ ๋ฐฐ์นํ ๋, ๋ถ๋ชจ๋ relative๋ก ์ก๊ณ ๊ทธ ๋ถ๋ชจ๋ฅผ ์ค์ฌ์ผ๋ก ์์น๋ฅผ ์ก๋ ์์ ์์๋ฅผ absolute๋ฅผ ์ค๋ค. ๋ฌผ๋ก ๊ผญ ๋ถ๋ชจ ์์๊ฐ relative์ผ ํ์๋ ์๋ค. ๋ค๋ง relative๊ฐ ๋ ์ด์์ ํ๋ฆ์ ์ํฅ์ ๋ฑํ ๋ฐ์ง ์์ผ๋ฉด์ ๊ธฐ์ค๋ง ์๊ธฐ๊ธฐ ๋๋ฌธ์ ์ง๊ด์ ์ด๋ผ์ ๊ทธ๋ ๊ฒ ๋ฐฐ์น๋ฅผ ํ๋ค๊ณ ํ๋ค.
width์ height๋ก ๋ฒํผ์ ํฌ๊ธฐ๋ฅผ ์ ํ๊ณ , top๊ณผ left, transform์ผ๋ก ๋ฒํผ์ด ์ ์ค์์ ์ค๋๋ก ๋ฐฐ์นํ๋ค.
4. ๋ฒํผ์ ๋๋ฅด๋ฉด ํ ์คํธ ๋ํ๋ด๊ธฐ
- import ํ๊ธฐ
<script>
import HelloVue from "@/components/HelloVue.vue";
export default {
name: "MainView",
components: {HelloView},
}
</script>
์๊น HelloView์์ export ํ์๋๋ฐ, ๊ทธ๊ฑธ MainView์์ import๋ก ๋ฐ์๋ณด๊ฒ ๋ค. from์ ๊ฒฝ๋ก๋ฅผ ์ ์ด์ค๋ค. ์ ์ฌ์ง์ ๋ณด๋ฉด components ๋๋ ํฐ๋ฆฌ ํ์์ HelloVue.vue๊ฐ ์๋ค. ๊ทธ๋์ "@/components/HelloVue.vue"์ด๋ ๊ฒ ๊ฒฝ๋ก๋ฅผ ์ค์ ํ๋ค. components๋ ๊ฐ์ ธ์จ ์ปดํฌ๋ํธ๋ฅผ ํ์ฌ ์ปดํฌ๋ํธ์ ๋ฑ๋กํ๋ ๊ฒ์ด๋ค.
- template HelloView ์ค์ ํ๊ธฐ
<template>
<div class="main-view">
<button class="btn" @click="btnHello" style="cursor: pointer"> Click </button>
<HelloView v-if="showHello" :show-hello="showHello"/>
</div>
</template>
v-if๋ ๋ธ๋ก์ ์กฐ๊ฑด๋ถ๋ก ๋ ๋๋ง ํ๋ ๋ฐ ์ฌ์ฉํ๋ค. ๊ณต์๋ฌธ์์ ๋ฐ๋ฅด๋ฉด ๋๋ ํฐ๋ธ์ ํํ์์ด ์ฐธ ๊ฐ์ ๋ฐํํ ๋๋ง ๋ธ๋ก์ด ๋ ๋๋ง ๋๋ค๊ณ ์ ํ์๋ค. ์ด ์ฌ์ค์ ์ ๊ธฐ์ตํ๊ณ ์์.
:show-hello:"showHello"๋ HelloView์์ Props์ ๋ฃ์๋ ๊ฒ์ด๋ค. props๋ ๋ถ๋ชจ ์ปดํฌ๋ํธ๊ฐ ์์์๊ฒ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ์๋จ์ด๋ผ๊ณ ์ค๋ช ํ์๋ค. ๋ฒํผ์ ๋๋ฅด๋ฉด true ๊ฐ์ด ์์ HelloView์ ์ ๋ฌ๋๋๋ฐ, ์ฌ์ค ์ด ์ฝ๋์์ ๊ผญ ํ์ํ ์กด์ฌ๋ ์๋ ๊ฒ ๊ฐ๋ค. ํ์ฌ ์ฝ๋๋ ๊ฐ๋จํ๊ฒ true์ธ์ง false์ธ์ง๋ก ๋์์ด ํํ ์ง ๋ง์ง ๊ฒฐ์ ์ด ๋๋ค. ๊ฐ์ ์์์๊ฒ ๋ณด๋ด์ฃผ๊ณ , ๊ทธ ๊ฐ์ ํตํด ์์ ๋ด๋ถ์ ์ด๋ค ๋ณํ๊ฐ ์ผ์ด๋์ง ์์ผ๋ ๊ตณ์ด ์ ์จ๋ ๋๋ ๋ถ๋ถ์ด๊ธด ํ๋ ํ์ ์ฌ์ฉํ ์๋ ์์ผ๋ ๋๋๊ธฐ๋ก ๊ฒฐ์ ํ๋ค.
- script์ ๋์ ๊ด๋ จ ์ฝ๋ ์ถ๊ฐํ๊ธฐ
export default {
name: "MainView",
components: {HelloView},
data() {
return {
showHello: false,
};
},
methods: {
btnHello() {
this.showHello = true;
}
},
}
</script>
data( )๋ ์ํ๋ฅผ ์ ์ํ๋ค. showHello์ ์ํ๋ false๋ก MainView ํ๋ฉด์ ๋ํ๋์ง ์๋๋ก ํ๋ค. methods๋ ๋ฒํผ์ ํด๋ฆญํ์ ๋ ์คํํ ํจ์๋ฅผ ์ ์ํ๋ค. this.showHello๋ก false๋ก ํ์๋ showHello๊ฐ true๋ก ๋ฐ๋๋๋ก ๋ง๋ค์๋ค. ์ฌ๊ธฐ์ this.๋ Vue์ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋ฉฐ data( )์ ์ ์ํ ๋ณ์๋ฅผ ์๋ฏธํ๋ค.
์๋์ ์ถ์ฒ๋ฅผ ๋จ๊ฒจ๋๊ฒ ์ต๋๋ค.
๋ธ๋ก๊ทธ์ ์ ๊ฐ ์ต๋ํ ์ดํดํ ๋ด์ฉ๊น์ง๋ง ์ ๋๊ฑฐ๋ผ
์ถ์ฒ์ ๋ค์ด๊ฐ์๋ฉด ๋ ๋ง์ ๋ด์ฉ์ด ์์ด ๊ณต๋ถํ์๋๋ฐ ๋์๋์ค๊ฒ๋๋ค.
๐ชฝ
ํ๋ฆฐ ๋ด์ฉ์ด ์๋ค๋ฉด ๋๊ธ๋ก ์๋ ค์ฃผ์ธ์
Vue.js
Vue.js - ํ๋ก๊ทธ๋์๋ธ ์๋ฐ์คํธ๋ฆฝํธ ํ๋ ์์ํฌ
ko.vuejs.org
Vue.js
Vue.js - ํ๋ก๊ทธ๋์๋ธ ์๋ฐ์คํธ๋ฆฝํธ ํ๋ ์์ํฌ
ko.vuejs.org
Vue.js
Vue.js - ํ๋ก๊ทธ๋์๋ธ ์๋ฐ์คํธ๋ฆฝํธ ํ๋ ์์ํฌ
ko.vuejs.org